問題描述
我正在使用以下代碼 (C# .NET 3.5) 上傳文件:
I am using below code (C# .NET 3.5) to upload a file:
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://someweb.mn/altanzulpharm/file12.zip");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.KeepAlive = true;
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
FileStream fs = File.OpenRead(FilePath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = request.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
但是當(dāng)互聯(lián)網(wǎng)中斷時(shí)上傳會(huì)中斷.中斷發(fā)生的時(shí)間非常短,幾乎是一毫秒.但是上傳會(huì)永遠(yuǎn)中斷!
But the upload breaks when internet interrupted. Interruption occurs for a very small amount of time, almost a millisecond. But uploading breaks forever!
網(wǎng)絡(luò)中斷后是否可以繼續(xù)或恢復(fù)上傳?
Is it possible to continue or resume uploading after interruption of internet?
推薦答案
FtpWebRequest
中斷連接后恢復(fù)傳輸?shù)奈ㄒ环椒ㄊ侵匦逻B接并開始寫入文件末尾.
The only way to resume transfer after a connection is interrupted with FtpWebRequest
, is to reconnect and start writing to the end of the file.
為此使用 FtpWebRequest.ContentOffset
.
完整代碼上傳的相關(guān)問題(盡管針對(duì) C#):
如何在斷開連接的情況下自動(dòng)恢復(fù)下載FTP文件
A related question for upload with full code (although for C#):
How to download FTP files with automatic resume in case of disconnect
或者使用可以自動(dòng)恢復(fù)傳輸?shù)?FTP 庫.
Or use an FTP library that can resume the transfer automatically.
例如 WinSCP .NET 程序集.有了它,可恢復(fù)的上傳就變得如此簡單:
For example WinSCP .NET assembly does. With it, a resumable upload is as trivial as:
// Setup session options
var sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "user",
Password = "mypassword"
};
using (var session = new Session())
{
// Connect
session.Open(sessionOptions);
// Resumable upload
session.PutFileToDirectory(@"C:pathfile.zip", "/home/user");
}
(我是 WinSCP 的作者)
這篇關(guān)于網(wǎng)絡(luò)中斷后如何繼續(xù)或恢復(fù) FTP 上傳的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!