0% found this document useful (0 votes)
27 views

Uploading A File To FTP

To upload a file to FTP in C#, one must provide the server's FTP URL, username, and password. The FtpWebRequest, WebRequestMethods, and NetworkCredential classes can be used to upload a file. Variables are declared for the source file path, FTP URL, username, and password. The UploadFileToFTP method opens the source file stream, writes the file contents to an FTP stream using FtpWebRequest, and closes the streams. This method is then called, providing the source file path to upload the file.

Uploaded by

Julio_Papel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Uploading A File To FTP

To upload a file to FTP in C#, one must provide the server's FTP URL, username, and password. The FtpWebRequest, WebRequestMethods, and NetworkCredential classes can be used to upload a file. Variables are declared for the source file path, FTP URL, username, and password. The UploadFileToFTP method opens the source file stream, writes the file contents to an FTP stream using FtpWebRequest, and closes the streams. This method is then called, providing the source file path to upload the file.

Uploaded by

Julio_Papel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Uploading a File to FTP in C#

In order to upload a file using FTP details, one should know the servers FTP URL, FTP username and FTP password. We can achieve the file uploading task by using the below three inbuilt classes of .NET: FtpWebRequest, WebRequestMethods, and NetworkCredential . To start with the coding part. First we need to import the below namespaces
using System.IO; using System.Net;

Then we need to declare four variables as below:


String String String String sourcefilepath = @absolutepath; // e.g. d:/test.docx ftpurl = @ftpurl; // e.g. ftp://serverip/foldername/foldername ftpusername = @ftpusername; // e.g. username ftppassword = @ftppassword; // e.g. password

Note: Please replace @absolutepath, @ftpurl, ftpusername, @ftppassword with your actual values. Copy the below code into your class:
private static void UploadFileToFTP(string source) { try { string filename = Path.GetFileName(source); string ftpfullpath = ftpurl; FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); ftp.Credentials = new NetworkCredential(ftpusername, ftppassword); ftp.KeepAlive = true; ftp.UseBinary = true; ftp.Method = WebRequestMethods.Ftp.UploadFile; FileStream fs = File.OpenRead(source); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); Stream ftpstream = ftp.GetRequestStream(); ftpstream.Write(buffer, 0, buffer.Length); ftpstream.Close(); } catch (Exception ex) { throw ex; } }

The final point will be calling the above procedure by using the below code on whatever event you want the operation to be performed.
UploadFileToFTP(sourcefilepath);

You might also like