簡體   English   中英

C#如何將大文件上傳到ftp(文件大小必須為500 MB-1 GB)

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

我是新手,我想使用C#將大文件發送到ftp,但是我無法發送500 MB-1 GB以上的文件。 有人可以幫我嗎? 謝謝。

我使用的代碼是:

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

不要按塊讀取整個文件(它會占用過多的內存和時間):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM