Created
April 3, 2018 09:50
-
-
Save talkingdotnet/438f89d9d2c74df8914adda54c3e4b7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { HttpClient, HttpRequest, HttpEventType, HttpResponse } from '@angular/common/http' | |
@Component({ | |
selector: 'upload-component', | |
templateUrl: './upload.component.html' | |
}) | |
export class UploadComponent { | |
public progress: number; | |
public message: string; | |
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 uploadReq = new HttpRequest('POST', `api/upload`, formData, { | |
reportProgress: true, | |
}); | |
this.http.request(uploadReq).subscribe(event => { | |
if (event.type === HttpEventType.UploadProgress) | |
this.progress = Math.round(100 * event.loaded / event.total); | |
else if (event.type === HttpEventType.Response) | |
this.message = event.body.toString(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment