Guest User

Untitled

a guest
Jul 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. class ImageSnippet {
  2. pending: boolean = false;
  3. status: string = 'init';
  4.  
  5. constructor(public src: string, public file: File) {}
  6. }
  7.  
  8. @Component({
  9. selector: 'bwm-image-upload',
  10. templateUrl: 'image-upload.component.html',
  11. styleUrls: ['image-upload.component.scss']
  12. })
  13. export class ImageUploadComponent {
  14.  
  15. selectedFile: ImageSnippet;
  16.  
  17. constructor(private imageService: ImageService){}
  18.  
  19. private onSuccess() {
  20. this.selectedFile.pending = false;
  21. this.selectedFile.status = 'ok';
  22. }
  23.  
  24. private onError() {
  25. this.selectedFile.pending = false;
  26. this.selectedFile.status = 'fail';
  27. this.selectedFile.src = '';
  28. }
  29.  
  30. processFile(imageInput: any) {
  31. const file: File = imageInput.files[0];
  32. const reader = new FileReader();
  33.  
  34. reader.addEventListener('load', (event: any) => {
  35.  
  36. this.selectedFile = new ImageSnippet(event.target.result, file);
  37.  
  38. this.selectedFile.pending = true;
  39. this.imageService.uploadImage(this.selectedFile.file).subscribe(
  40. (res) => {
  41. this.onSuccess();
  42. },
  43. (err) => {
  44. this.onError();
  45. })
  46. });
  47.  
  48. reader.readAsDataURL(file);
  49. }
  50. }
Add Comment
Please, Sign In to add comment