Upload File to Ftp Using C#

Upload file to FTP using C#

The existing answers are valid, but why re-invent the wheel and bother with lower level WebRequest types while WebClient already implements FTP uploading neatly:

using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile);
}

Upload file on FTP

Please make sure your ftp path is set as shown below.

string CompleteDPath = "ftp://www.example.com/wwwroot/videos/";

string FileName = "sample.mp4";

WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName);

The following script work great with me for uploading files and videos to another servier via ftp.

FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(ftpurl + "" + username + "_" + filename);
ftpClient.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
ftpClient.UseBinary = true;
ftpClient.KeepAlive = true;
System.IO.FileInfo fi = new System.IO.FileInfo(fileurl);
ftpClient.ContentLength = fi.Length;
byte[] buffer = new byte[4097];
int bytes = 0;
int total_bytes = (int)fi.Length;
System.IO.FileStream fs = fi.OpenRead();
System.IO.Stream rs = ftpClient.GetRequestStream();
while (total_bytes > 0)
{
bytes = fs.Read(buffer, 0, buffer.Length);
rs.Write(buffer, 0, bytes);
total_bytes = total_bytes - bytes;
}
//fs.Flush();
fs.Close();
rs.Close();
FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
value = uploadResponse.StatusDescription;
uploadResponse.Close();

Upload a file to an FTP server from a string or stream

Just copy your stream to the FTP request stream:

Stream requestStream = ftpRequest.GetRequestStream();
stream.CopyTo(requestStream);
requestStream.Close();

For a string (assuming the contents is a text):

byte[] bytes = Encoding.UTF8.GetBytes(data);

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}

Or even better use the StreamWriter:

using (Stream requestStream = request.GetRequestStream())
using (StreamWriter writer = new StreamWriter(requestStream, Encoding.UTF8))
{
writer.Write(data);
}

If the contents is a text, you should use the text mode:

request.UseBinary = false;

c# / More effective way to upload file with ftp

Try to use FtpWebRequest and WebRequestMethods.Ftp.UploadFile. Here is the piece of code which we use for uplaoding files from ZipArchive to FTP (so there is also an option showing creating a directories if you need it too). From all methods which I've tested it was the most efficient one.

//// Get the object used to communicate with the server.
var request =
(FtpWebRequest)
WebRequest.Create("ftp://" + ftpServer + @"/" + remotePath + @"/" +
entry.FullName.TrimEnd('/'));

//// Determine if we are transferring file or directory
if (string.IsNullOrWhiteSpace(entry.Name) && !string.IsNullOrWhiteSpace(entry.FullName))
request.Method = WebRequestMethods.Ftp.MakeDirectory;
else
request.Method = WebRequestMethods.Ftp.UploadFile;

//// Try to transfer file
try
{
//// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(user, password);

switch (request.Method)
{
case WebRequestMethods.Ftp.MakeDirectory:
break;

case WebRequestMethods.Ftp.UploadFile:
var buffer = new byte[8192];
using (var rs = request.GetRequestStream())
{
StreamUtils.Copy(entry.Open(), rs, buffer);
}

break;
}
}
catch (Exception ex)
{
//// Handle it!
LogHelper.Error<FtpHelper>("Could not extract file from package.", ex);
}
finally
{
//// Get the response from the FTP server.
var response = (FtpWebResponse) request.GetResponse();

//// Close the connection = Happy a FTP server.
response.Close();
}

C# How to use FTP upload File Class

The URL has this format:

WebRequest.Create("ftp://ftp.example.com/path/to/directory/filename.ext");

Upload and download a file to/from FTP server in C#/.NET


Upload

The most trivial way to upload a binary file to an FTP server using .NET framework is using WebClient.UploadFile:

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, transfer resuming, etc), use FtpWebRequest. Easy way is to just copy a FileStream to FTP stream using Stream.CopyTo:

FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}

If you need to monitor an upload progress, you have to copy the contents by chunks yourself:

FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
byte[] buffer = new byte[10240];
int read;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
ftpStream.Write(buffer, 0, read);
Console.WriteLine("Uploaded {0} bytes", fileStream.Position);
}
}

For GUI progress (WinForms ProgressBar), see:

How can we show progress bar for upload with FtpWebRequest

If you want to upload all files from a folder, see

Recursive upload to FTP server in C#.



Download

The most trivial way to download a binary file from an FTP server using .NET framework is using WebClient.DownloadFile:

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, resuming transfers, etc), use FtpWebRequest. Easy way is to just copy an FTP response stream to FileStream using Stream.CopyTo:

FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
ftpStream.CopyTo(fileStream);
}

If you need to monitor a download progress, you have to copy the contents by chunks yourself:

FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}

For GUI progress (WinForms ProgressBar), see:

FtpWebRequest FTP download with ProgressBar

If you want to download all files from a remote folder, see

C# Download all files and subdirectories through FTP.



Related Topics



Leave a reply



Submit