我是个新手,我想用C#向ftp发送一个大文件,但是我不能发送超过500 MB -1GB的文件。有人能帮帮我吗?谢谢。
我使用的代码是:
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();
}发布于 2013-12-12 04:03:03
不要读取整个文件(它会消耗太多的内存和时间),按块读取:
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呢?
发布于 2021-08-09 10:51:17
我认为你也需要dlls来使转会变得更容易。此外,异步套接字将防止在传输过程中出现此类故障错误。
https://stackoverflow.com/questions/20534699
复制相似问题