简体   繁体   English

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

[英]C# How to upload large file to ftp ( File size must 500 MB - 1 GB)

I'm a newbie, I want to send a large file to ftp using C# but I can't send files above 500 MB - 1 GB. 我是新手,我想使用C#将大文件发送到ftp,但是我无法发送500 MB-1 GB以上的文件。 Could someone please help me? 有人可以帮我吗? Thank you. 谢谢。

The code I'm using is: 我使用的代码是:

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

Do not read the whole file (it will consume too much memory and time), read by blocks: 不要按块读取整个文件(它会占用过多的内存和时间):

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

And yes, what about FtpWebRequest ? 是的, FtpWebRequest呢?

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

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