简体   繁体   English

网络中断后如何继续或恢复FTP上传

[英]How to continue or resume FTP upload after interruption of internet

I am using below code (C# .NET 3.5) to upload a file:我正在使用以下代码(C# .NET 3.5)上传文件:

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

But the upload breaks when internet interrupted.但是当互联网中断时上传中断。 Interruption occurs for a very small amount of time, almost a millisecond.中断发生的时间非常短,几乎是一毫秒。 But uploading breaks forever!但是上传永远中断!

Is it possible to continue or resume uploading after interruption of internet?互联网中断后是否可以继续或恢复上传?

I don't believe FtpWebRequest supports re-connection after losing connection.我不相信 FtpWebRequest 支持在失去连接后重新连接。 You can resume upload from given position if server supports it (this support is not required and presumably less common that retry for download).如果服务器支持,您可以从给定位置恢复上传(此支持不是必需的,并且在重试下载时可能不太常见)。

You'll need to set FtpWebRequet.ContentOffset upload.您需要设置FtpWebRequet.ContentOffset上传。 Part of sample from the article:文章部分样本:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.ContentOffset = offset;

Internal details of restore in FTP protocol itself - RFC959: 3.5 - Error recovery and restart . FTP 协议本身中恢复的内部细节 - RFC959: 3.5 - Error recovery and restart Question showing retry code for download - Downloading from FTP with c#, can't retry when fail显示下载重试代码的问题 - 使用 c# 从 FTP 下载,失败时无法重试

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中断后恢复传输的唯一方法是重新连接并开始写入文件末尾。

For that use FtpWebRequest.ContentOffset .为此,请使用FtpWebRequest.ContentOffset

A related question for upload with full code (although for C#):使用完整代码上传的相关问题(尽管对于 C#):
How to download FTP files with automatic resume in case of disconnect如何在断开连接的情况下自动恢复下载 FTP 文件


Or use an FTP library that can resume the transfer automatically.或者使用可以自动恢复传输的 FTP 库。

For example WinSCP .NET assembly does.例如WinSCP .NET 程序集 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:\path\file.zip", "/home/user");
}

(I'm the author of WinSCP) (我是 WinSCP 的作者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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