首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#如何将大文件上传到ftp (文件大小必须为500 MB -1GB)

C#如何将大文件上传到ftp (文件大小必须为500 MB -1GB)
EN

Stack Overflow用户
提问于 2013-12-12 03:52:46
回答 2查看 502关注 0票数 0

我是个新手,我想用C#向ftp发送一个大文件,但是我不能发送超过500 MB -1GB的文件。有人能帮帮我吗?谢谢。

我使用的代码是:

代码语言:javascript
复制
    private void btnUpload_Click_1(object sender, EventArgs e)
    {
        openFileDialog.ShowDialog();
        NetworkStream passiveConnection;
        FileInfo fileParse = new FileInfo(openFileDialog.FileName);
        FileStream fs = new
        FileStream(openFileDialog.FileName, FileMode.Open);
        byte[] fileData = new byte[fs.Length];
        fs.Read(fileData, 0, (int)fs.Length);
        passiveConnection = createPassiveConnection();
        string cmd = "STOR " + fileParse.Name + "\r\n";
        tbStatus.Text += "\r\nSent:" + cmd;
        string response = sendFTPcmd(cmd);
        tbStatus.Text += "\r\nRcvd:" + response;
        passiveConnection.Write(fileData, 0, (int)fs.Length);
        passiveConnection.Close();
        MessageBox.Show("Uploaded");
        tbStatus.Text += "\r\nRcvd:" + new
        StreamReader(NetStrm).ReadLine(); getRemoteFolders();
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-12 04:03:03

不要读取整个文件(它会消耗太多的内存和时间),按块读取:

代码语言:javascript
复制
NetworkStream passiveConnection;
FileInfo fileParse = new FileInfo(openFileDialog.FileName);
using(FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open))
{
     byte[] buf = new byte[8192];
     int read;

     passiveConnection = createPassiveConnection();
     string cmd = "STOR " + fileParse.Name + "\r\n";
     tbStatus.Text += "\r\nSent:" + cmd;
     string response = sendFTPcmd(cmd);
     tbStatus.Text += "\r\nRcvd:" + response;

     while ((read = fs.Read(buf, 0, buf.Length) > 0)
     {
         passiveConnection.Write(buf, 0, read);
     }
}

passiveConnection.Close();
MessageBox.Show("Uploaded");
tbStatus.Text += "\r\nRcvd:" + new
StreamReader(NetStrm).ReadLine(); 
getRemoteFolders();

是的,FtpWebRequest呢?

票数 1
EN

Stack Overflow用户

发布于 2021-08-09 10:51:17

我认为你也需要dlls来使转会变得更容易。此外,异步套接字将防止在传输过程中出现此类故障错误。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20534699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档