网络中断后如何继续或恢复 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 上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)