Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2.0 and Angular 4.3 File Upload with progress

Using the new Angular 4.3 HttpClient, how can I upload and access files in an ASP.NET Core 2.0 Controller while reporting upload progress to the client?

like image 256
Matthew Steven Monkan Avatar asked Sep 13 '17 20:09

Matthew Steven Monkan


People also ask

How to upload files from angular to ASP NET Core backend?

This article discuss about uploading files from an Angular application to ASP.NET Core backend. First you need to create an ASP.NET Core Angular project. Once it is done, you need to create an angular component. And then you can modify the backend implementation.

How do I download the finished project in angular?

If you want to download our finished project, you can do that from Upload Files .NET Core Angular Finished Project. We are going to divide this article into the following sections:

How to create an angular component in ASP NET Core?

First you need to create an ASP.NET Core Angular project. Once it is done, you need to create an angular component. And then you can modify the backend implementation. To create an angular component, you need to run the following command - ng generate component fileupload --skip-import --skip-tests -s.

How to read and save in ASP NET Core?

In the next section you will learn how to read and save in ASP.NET Core. In ASP.NET Core you can create a controller, and create an action method. In the action method, you can use Request.Form.Files to read the uploaded files with Form Data from Angular client. The above code is using the Request.Form.Files options.


1 Answers

Here is a working example to get started:

HTML

<input #file type="file" multiple (change)="upload(file.files)" />
<span *ngIf="uploadProgress > 0 && uploadProgress < 100">
    {{uploadProgress}}%
</span>

TypeScript

import { Component } from '@angular/core';
import { HttpClient, HttpRequest, HttpEventType, HttpResponse } from '@angular/common/http'

@Component({
    selector: 'files',
    templateUrl: './files.component.html',
})
export class FilesComponent {
    public uploadProgress: number;

    constructor(private http: HttpClient) { }

    upload(files) {
        if (files.length === 0)
            return;

        const formData = new FormData();

        for (let file of files)
            formData.append(file.name, file);

        const req = new HttpRequest('POST', `api/files`, formData, {
            reportProgress: true,
        });

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

Controller

[HttpPost, DisableRequestSizeLimit, Route("api/files")]
public async Task UploadFiles()
{
    var files = Request.Form.Files; // now you have them
}
like image 159
Matthew Steven Monkan Avatar answered Nov 13 '22 19:11

Matthew Steven Monkan