4

I am developing an windows application for backup(Files and sql server database). Now i need to upload these files(.rar files) to my ftp site. For uploading i use this code.

Code

string file = "D:\\RP-3160-driver.zip";
//opening the file for read.
string uploadFileName = "", uploadUrl = "";
uploadFileName = new FileInfo(file).Name;
uploadUrl = "ftp://ftp.Sitename.com/tempFiles/";
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
try
{
   long FileSize = new FileInfo(file).Length; // File size of file being uploaded.

   Byte[] buffer = new Byte[FileSize];
   fs.Read(buffer, 0, buffer.Length);
   fs.Close();
   fs = null;

   string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
   FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
   requestObj.Method = WebRequestMethods.Ftp.UploadFile;
   requestObj.Credentials = new NetworkCredential("usernam", "password");
   Stream requestStream = requestObj.GetRequestStream();
   requestStream.Write(buffer, 0, buffer.Length);
   requestStream.Flush();
   requestStream.Close();
   requestObj = null;
   MessageBox.Show("File upload/transfer Successed.", "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
   if (fs != null)
   {
      fs.Close();
   }
   MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

This code uploads only those files which has size < 5 Mb. But i need to upload larger then 500Mb to 1Gb files. So can any one help me.

Community
  • 1
  • 1
Ashish Rathore
  • 2,546
  • 9
  • 55
  • 91

2 Answers2

9

For larger files you may choose to read the file stream and write it to the output stream as you read it.

FileStream fs = null;
Stream rs = null;

try 
{
    string file = "D:\\RP-3160-driver.zip";
    string uploadFileName = new FileInfo(file).Name;
    string uploadUrl = "ftp://ftp.Sitename.com/tempFiles/";
    fs = new FileStream(file, FileMode.Open, FileAccess.Read);

    string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
    FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
    requestObj.Method = WebRequestMethods.Ftp.UploadFile;
    requestObj.Credentials = new NetworkCredential("usernam", "password");
    rs = requestObj.GetRequestStream();

    byte[] buffer = new byte[8092];
    int read = 0;
    while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
    {
       rs.Write(buffer, 0, read);
    }
    rs.Flush();
}
catch (Exception exception) 
{

    MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + exception.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally 
{
    if (fs != null)
    {
        fs.Close();
        fs.Dispose();
    }

    if (rs != null)
    {
        rs.Close();
        rs.Dispose();
    }
}
Sanpas
  • 1,170
  • 10
  • 29
  • I have used same code for transfer but application throw "Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host." Upload data size is: -310MB – Abhay.Patil Sep 27 '19 at 10:50
  • @Abhay.Patil Probably there is a setting in the FTP server-side that restricts the upload of files that are bigger than 310MB. Are you using IIS to set up an FTP server? – Nithin B Jun 14 '20 at 04:45
4

Stream class has a nice Method CopyTo.

You don't need to read and write from/to streams. Just use fs.CopyTo(requestStream);

With this method, you don't have to declare large arrays like new Byte[FileSize];

I4V
  • 34,891
  • 6
  • 67
  • 79
  • Man you are the angel in my life! Seriusly thank you! – khaosdoctor Apr 13 '15 at 18:19
  • I was worried about setting the FtpWebRequest.ContentLength Property, but apparently the value [is ignored by the FtpWebRequest class](https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.contentlength(v=vs.110).aspx) – mcdon Jul 24 '18 at 20:39