简体   繁体   中英

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. 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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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