Created
July 17, 2018 21:41
-
-
Save Jerga99/e87bb1ed9d7a2f4fc5c037d00aa4ba05 to your computer and use it in GitHub Desktop.
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
class ImageSnippet { | |
pending: boolean = false; | |
status: string = 'init'; | |
constructor(public src: string, public file: File) {} | |
} | |
@Component({ | |
selector: 'bwm-image-upload', | |
templateUrl: 'image-upload.component.html', | |
styleUrls: ['image-upload.component.scss'] | |
}) | |
export class ImageUploadComponent { | |
selectedFile: ImageSnippet; | |
constructor(private imageService: ImageService){} | |
private onSuccess() { | |
this.selectedFile.pending = false; | |
this.selectedFile.status = 'ok'; | |
} | |
private onError() { | |
this.selectedFile.pending = false; | |
this.selectedFile.status = 'fail'; | |
this.selectedFile.src = ''; | |
} | |
processFile(imageInput: any) { | |
const file: File = imageInput.files[0]; | |
const reader = new FileReader(); | |
reader.addEventListener('load', (event: any) => { | |
this.selectedFile = new ImageSnippet(event.target.result, file); | |
this.selectedFile.pending = true; | |
this.imageService.uploadImage(this.selectedFile.file).subscribe( | |
(res) => { | |
this.onSuccess(); | |
}, | |
(err) => { | |
this.onError(); | |
}) | |
}); | |
reader.readAsDataURL(file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment