猿问

如何使用 C# 将大文件上传到另一个 HTTP 服务?

我正在开发一个由 C# 在 .NET Core 2.2 上编写的项目。在我的代码中,我需要将一个大文件(大约 2GB)上传到远程 HTTP 服务器。


我不负责远程服务器。我只负责上传逻辑。


在 C# 中通过 HTTP 上传大型本地文件的最佳做法是什么?


我试过下面的代码


https://github.com/AiursoftWeb/Nexus/blob/master/Pylon/Services/HTTPService.cs#L66-L81


    public async Task<string> PostFile(string url, string filepath)

    {

        var request = new HttpClient();

        var form = new MultipartFormDataContent();

        string responseString = null;

        using (var fileStream = new FileStream(filepath, mode: FileMode.Open))

        {

            using (var bufferedStream = new BufferedStream(fileStream))

            {

                form.Add(new StreamContent(bufferedStream), "file", new FileInfo(filepath).FullName);

                var response = await request.PostAsync(url, form);

                responseString = await response.Content.ReadAsStringAsync();

                fileStream.Close();

            }

        }

        return responseString;

    }

我在该代码中找不到任何错误,它适用于小文件。(约500MB)


但是如果我上传一个大约2GB的大文件就会引发异常:


操作被取消。将内容复制到流时出错。无法访问已处置的对象。对象名称:'SslStream'。


我猜流在上传时被处理掉了。


我希望我的整个文件可以成功上传到远程服务器,并且该过程不会占用我的大部分 RAM。那么我的代码有什么问题呢?有没有适合我的例子?


附加信息:


我查看了上游 HTTP 服务器的日志,显示连接已被远程客户端终止。连接持续了 1.7 分钟才关闭。


日志说:


现有连接被远程主机强制关闭 现有连接被远程主机强制关闭


回首忆惘然
浏览 156回答 1
1回答

ABOUTYOU

我发现了问题:HTTP 客户端的默认超时是 100 秒。源代码在这里:https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpClient.cs#L17我已经像这样更改了我的代码:&nbsp; &nbsp; &nbsp; &nbsp; public async Task<string> PostFile(string url, string filepath)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var request = new HttpClient&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timeout = TimeSpan.FromSeconds(3600)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var form = new MultipartFormDataContent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string responseString = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var fileStream = new FileStream(filepath, mode: FileMode.Open))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var bufferedStream = new BufferedStream(fileStream))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.Add(new StreamContent(bufferedStream), "file", new FileInfo(filepath).FullName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var response = await request.PostAsync(url, form);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; responseString = await response.Content.ReadAsStringAsync();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileStream.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return responseString;&nbsp; &nbsp; &nbsp; &nbsp; }问题解决了。
随时随地看视频慕课网APP
我要回答