- using System;
- using System.IO;
- using System.Net;
- class Program
- {
- static void Main(string[] args)
- {
- string FTPAddress = "";
- string filename = "samplefile.txt";
- string username = "";
- string password = "";
- try
- {
- FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://"
- + FTPAddress +"/samplefile.txt");
- request.Method = WebRequestMethods.Ftp.UploadFile;
- request.Credentials = new NetworkCredential(username, password);
- request.UsePassive = true;
- request.UseBinary = true;
- request.KeepAlive = false;
- FileStream stream = File.OpenRead(filename);
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- stream.Close();
- Stream reqStream = request.GetRequestStream();
- reqStream.Write(buffer, 0, buffer.Length);
- reqStream.Close();
- Console.WriteLine("Upload Complete");
- }
- catch (WebException e)
- {
- Console.WriteLine(e.Message);
- }
- Console.ReadKey();
- }
- }
File Upload Using FtpWebRequest In C#
This snippet shows how to upload a file to an ftp server using the FtpWebRequest Class.
No comments:
Post a Comment