Skip to content

Instantly share code, notes, and snippets.

@Jerga99
Created July 17, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jerga99/e87bb1ed9d7a2f4fc5c037d00aa4ba05 to your computer and use it in GitHub Desktop.
Save Jerga99/e87bb1ed9d7a2f4fc5c037d00aa4ba05 to your computer and use it in GitHub Desktop.
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