Created
June 14, 2020 02:12
-
-
Save geeksilva97/48fa4a51f87719479a60858bc44d2041 to your computer and use it in GitHub Desktop.
Upload file
This file contains 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, OnInit, ViewChild, ElementRef } from '@angular/core'; | |
import { HttpClient, HttpEventType } from '@angular/common/http'; | |
@Component({ | |
selector: 'app-home', | |
templateUrl: './home.component.html', | |
styleUrls: ['./home.component.scss'] | |
}) | |
export class HomeComponent implements OnInit { | |
@ViewChild('inputFile') | |
inputFile: ElementRef; | |
@ViewChild('progressBar') | |
progressBar: ElementRef; | |
uploadStatus: string = ''; | |
status: string = ''; | |
constructor( | |
private http: HttpClient | |
) { } | |
ngOnInit(): void { | |
} | |
sendFile() { | |
let file: File = this.inputFile.nativeElement.files[0]; | |
let formData = new FormData(); | |
formData.append("file", file, "video.mov"); | |
this.http.post('http://localhost:5000/profile/upload', formData, { | |
observe: 'events', | |
reportProgress: true | |
}).subscribe(event => { | |
switch (event.type) { | |
case HttpEventType.UploadProgress: | |
console.log('Uploading....'); | |
let p = (event.loaded / event.total) * 100; | |
(<HTMLProgressElement> this.progressBar.nativeElement).value = Math.round(p); | |
this.uploadStatus = "Uploaded " + event.loaded + " bytes of " + event.total; | |
break; | |
case HttpEventType.Response: | |
console.log('Response', event.status, event.body); | |
this.status = JSON.stringify(event.body); | |
break; | |
default: | |
return `File "x" surprising upload event: ${event.type}.`; | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment