C#如何将大文件上传到FTP站点 [英] C# How to upload large files to ftp site

查看:257
本文介绍了C#如何将大文件上传到FTP站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于备份的Windows应用程序(Files and sql server database)。
现在我需要将这些文件(.rar文件)上传到我的ftp站点。
上传我使用这段代码。

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.

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文件。
所以任何人都可以帮助我。

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.

推荐答案

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

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 e) 
{
    MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally 
{
    if (fs != null)
    {
        fs.Close();
        fs.Dispose();
    }

    if (rs != null)
    {
        rs.Close();
        rs.Dispose();
    }
}

这篇关于C#如何将大文件上传到FTP站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆