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

标签 c# upload ftp windows-applications

我正在开发用于备份的 Windows 应用程序(文件和 sql server 数据库)。 现在我需要将这些文件(.rar 文件)上传到我的 ftp 站点。 对于上传,我使用此代码。

代码

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

此代码仅上传大小 < 5 Mb 的文件。 但我需要上传大于 500Mb 到 1Gb 的文件。 任何人都可以帮助我。

最佳答案

对于较大的文件,您可以选择读取文件流并在读取时将其写入输出流。

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

关于C#如何上传大文件到ftp站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16059157/

相关文章:

android - Android 上的 Phonegap : FileTransfer. 上传()失败

ruby-on-rails-3 - Rails 3 - Amazon S3 Paperclip EU 问题

linux - Docker:如何部署 vsftpd 实例?

c# - 点击 ListView 项目上的手势

c# - 多行字符串变量

c# - C# 中的正则表达式字符串缩减器?

php - 保存从 Android 发送的多部分图像文件到 Laravel 5.1

c# - 将字符串转换为 datetime2

http - 通过网络传输文件的最快方式是什么(FTP、HTTP、RSync 等)

Java 使用 JakartaFtpWrapper 上传 jpg - 使文件不可读