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

How to continue or resume FTP upload after interruption of internet(网络中断后如何继续或恢复 FTP 上传)
本文介绍了网络中断后如何继续或恢复 FTP 上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用以下代码 (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();

但是当互联网中断时上传会中断.中断发生的时间非常短,几乎是一毫秒.但是上传会永远中断!

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?

推荐答案

FtpWebRequest中断连接后恢复传输的唯一方法是重新连接并开始写入文件末尾.

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.

完整代码上传的相关问题(尽管针对 C#):
如何在断开连接的情况下自动恢复下载FTP文件

A related question for upload with full code (although for 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.

例如 WinSCP .NET 程序集.有了它,可恢复的上传就变得如此简单:

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 的作者)

这篇关于网络中断后如何继续或恢复 FTP 上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
How to generate password_hash for RabbitMQ Management HTTP API(如何为 RabbitMQ 管理 HTTP API 生成密码哈希)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Wait for a single RabbitMQ message with a timeout(等待一条带有超时的 RabbitMQ 消息)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)