简体   繁体   English

C#如何上传大文件到ftp站点

[英]C# How to upload large files to ftp site

I am developing an windows application for backup(Files and sql server database).我正在开发一个用于备份的 Windows 应用程序(文件和 sql server 数据库)。 Now i need to upload these files(.rar files) to my ftp site.现在我需要将这些文件(.rar 文件)上传到我的 ftp 站点。 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.此代码仅上传大小 < 5 Mb 的文件。 But i need to upload larger then 500Mb to 1Gb files.但我需要将大于 500Mb 的文件上传到 1Gb 文件。 So can any one help me.所以任何人都可以帮助我。

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();
    }
}

Stream class has a nice Method CopyTo . Stream类有一个很好的方法CopyTo

You don't need to read and write from/to streams.您不需要从/向流读取和写入。 Just use fs.CopyTo(requestStream);只需使用fs.CopyTo(requestStream);

With this method, you don't have to declare large arrays like new Byte[FileSize];使用这种方法,您不必像new Byte[FileSize];那样声明大型数组new Byte[FileSize];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM