我是新手,我想使用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();
}
答案 0 :(得分:1)
不要读取整个文件(它会消耗太多内存和时间),按块读取:
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 :(得分:-1)
我认为您还需要 dll 以使传输更容易。 异步套接字也可以防止传输过程中出现此类失败错误。