4

Based on this question and answer ASP.NET Core 2.0 and Angular 4.3 File Upload with progress

ASP.NET returns HTTP events - upload progress and final response:

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

And Angular accepts the response(s) and handles that:

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!');
        });

My questions:

1) What part of the above ASP.NET code tells that it has to respond UploadProgress until the upload process is done? How does it know that it is a request that needs to respond the UploadProgress? Where is the code in the ASP.NET source code, that return UploadProgress? (https://github.com/aspnet/AspNetCore)

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

AntonIva
  • 597
  • 1
  • 6
  • 22

1 Answers1

2

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

None of it. ASP.NET Core doesn't do this.

  • HttpPost indicates that you want to accept data in the form of a POST request.
  • DisableRequestSizeLimit indicates that you don't want ASP.NET Core to block file uploads over the pre-defined limit (30 MB, I believe).
  • Route defines the URL for the route.

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

No, it isn't. It's not necessary. The client knows how much data it has successfully sent, and this is where progress comes from.

The Angular HTTP library is built around XMLHttpRequest, which has a 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...

So the Angular HTTP library just hooks into the progress events of the local request object, and then provides them to you in a neater way.

Community
  • 1
  • 1
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86