gpt4 book ai didi

c# - ASP.NET 如何报告 UploadProgress?

转载 作者:太空狗 更新时间:2023-10-29 17:55:52 38 4
gpt4 key购买 nike

基于这个问答 ASP.NET Core 2.0 and Angular 4.3 File Upload with progress

ASP.NET 返回 HTTP 事件 - 上传进度和最终响应:

[HttpPost, DisableRequestSizeLimit, Route("api/files")]
public async Task UploadFiles()
{
var files = Request.Form.Files; // now you have them
}

Angular 接受响应并处理:

this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress)
this.uploadProgress = Math.round(100 * event.loaded / event.total);
else if (event instanceof HttpResponse)
console.log('Files uploaded!');
});

我的问题:

1) 上面的 ASP.NET 代码的哪一部分告诉它必须在上传过程完成之前响应 UploadProgress?它怎么知道这是一个需要响应UploadProgress的请求呢? ASP.NET 源代码中返回 UploadProgress 的代码在哪里? ( https://github.com/aspnet/AspNetCore )

2)整体流程,这是HTTP规范吗?显然,有一条规则,但到目前为止我找不到任何关于它的文档。

最佳答案

What part of the above ASP.NET code tells that it has to respond UploadProgress

都没有。 ASP.NET Core 不会这样做。

  • HttpPost 表示您要以 POST 请求的形式接受数据。
  • DisableRequestSizeLimit 表示您不希望 ASP.NET Core 阻止超过预定义限制(我相信是 30 MB)的文件上传。
  • Route 定义路由的 URL。

Overall flow, is this an HTTP specification? Clearly, there is a rule and I can't find any documentation about it so far.

不,不是。这不是必需的。客户端知道它已成功发送了多少数据,这就是进度的来源。

Angular HTTP 库是围绕 XMLHttpRequest 构建的,它有一个 Progress event :

The progress event handler, specified by the updateProgress() function in this example, receives the total number of bytes to transfer as well as the number of bytes transferred so far in the event's total and loaded fields. However, if the lengthComputable field is false, the total length is not known and will be zero.

Progress events exist for both download and upload transfers. The download events are fired on the XMLHttpRequest object itself, as shown in the above sample. The upload events are fired on the XMLHttpRequest.upload object...

因此 Angular HTTP 库只是挂接到本地请求对象的进度事件,然后以更简洁的方式提供给您。

关于c# - ASP.NET 如何报告 UploadProgress?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353342/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com