Asp.Net Core 5 API Web以流模式(如WCF)上传大文件 [英] Asp.Net Core 5 API Web upload large file in streamed mode like WCF

查看:73
本文介绍了Asp.Net Core 5 API Web以流模式(如WCF)上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以流模式(一次一个)在asp .net core 5制成的Web服务器上上传一个大文件(一次7GB).

I need to upload a large file (7GB) in streaming mode (one piece at a time) on my web server made in asp .net core 5.

配置服务器端:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>().UseKestrel(options =>
                    {
                        options.Limits.MaxRequestBodySize = long.MaxValue;
                       
                    });
                });
    }

服务器端的控制器:

[HttpPost]
[RequestFormLimits(MultipartBodyLengthLimit = Int32.MaxValue)]
public async Task PostStream(IFormFile formFiles)
{
    //rest of code
}

客户端(桌面应用程序)

Client Side ( Desktop app )

using (var httpClient = new HttpClient())
                {
                    using (var content = new MultipartFormDataContent())
                    {
                        string fileName = @"D:\largeFile.zip";

                        FileStream fileStream = File.OpenRead(fileName);
                        HttpContent fileStreamContent = new StreamContent(fileStream);

                        fileStreamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "formFiles", FileName = fileName };
                        fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

                        content.Add(fileStreamContent);

                       
                        var response = httpClient.PostAsync("http://localhost:5000/weatherforecast", content).Result;
                        response.EnsureSuccessStatusCode();
                }

系统最高可使用1 GB,具有大文件(7GB),服务器取消了请求,我注意到发送到服务器的文件不是流式传输,而是完全上传,我期望WCF上发生在服务器上的渐进式上载.如何逐步将文件发送到服务器?

The system works up to 1 GB, with large files (7GB) the server cancels the request, I noticed that the files that are sent to the server are not streaming but upload them completely, I was expecting a progressive upload on the server as it happens on WCF. How can i send a file to the server progressively?

推荐答案

我解决了这个问题,但是流必须独立存在,我无法将流中的附加数据包含在类或变量中,该类或变量与流一起传播

I solved it but the stream must stand alone I cannot accompany the stream with additional data present in a class or variable that travels together with the stream

客户端:

            using (var client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromMinutes(16);
                string fileName = @"D:\BIGFILE7GB.zip";
                var fileStreamContent = new StreamContent(File.OpenRead(fileName));

                var response = client.PostAsync("http://localhost:5000/weatherforecast/upload", fileStreamContent).Result;
                response.EnsureSuccessStatusCode();
            }

服务器端

public async Task<IActionResult> Upload()
        {
           //stream are here!!!!! 
           //Request.Body.Read() <------ magic

           // routine to save file
           while ((len = await Request.Body.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                file.Write(buffer, 0, len);
            }
            

            await Task.Delay(1);
            return Ok();
        }

解决方案是选择仅发送流(这样,控件将立即单击,而不仅是在文件完全加载之后才单击),还是仅发送表单数据.问题是是否有一种方法可以发送流并一起形成数据?是否始终遵守通过启动流传输流而不是在客户端上载整个文件时立即启动服务器端控制器操作的约束?

The solution is to choose whether to send only the streaming (so that the control clicks immediately and not only after the file has been completely loaded) or only the form data. The question is is there a way to send the stream and also form data together? Always respecting the constraint of immediately starting the action of the server-side controller by starting the streaming stream and not when the whole file has been uploaded on the client side?

像这样的解决方案:

public async Task<IActionResult> Upload(MyClass class)
{

    Read variable from class form and read buffer in straming togheter
}

这篇关于Asp.Net Core 5 API Web以流模式(如WCF)上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆