Advertisement
ricardomelocastro

Untitled

Feb 20th, 2018
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   photoModalOutputResult(item: any) {
  2.  
  3.     let tempBlob = this.dataURItoBlob(item);
  4.     let file = this.blobToFile(tempBlob, this.croppedFileName + ".png");
  5.     this.modalService.close('user-photo-edit-modal');
  6.     this.uploadPhoto(file);
  7.  
  8.   }
  9.  
  10.   public blobToFile(theBlob: Blob, fileName: string): File {
  11.     var b: any = theBlob;
  12.     b.lastModifiedDate = new Date();
  13.     b.name = fileName;
  14.  
  15.     //Cast to a File() type
  16.     return <File>theBlob;
  17.   }
  18.  
  19.  
  20.   protected dataURItoBlob(dataURI): Blob {
  21.     // convert base64 to raw binary data held in a string
  22.     // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
  23.     var byteString = atob(dataURI.split(',')[1]);
  24.  
  25.     // separate out the mime component
  26.     var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
  27.  
  28.     // write the bytes of the string to an ArrayBuffer
  29.     var ab = new ArrayBuffer(byteString.length);
  30.     var ia = new Uint8Array(ab);
  31.     for (var i = 0; i < byteString.length; i++) {
  32.       ia[i] = byteString.charCodeAt(i);
  33.     }
  34.  
  35.     // write the ArrayBuffer to a blob, and you're done
  36.     var bb = new Blob([ab], { type: 'application/pdf' });
  37.     return bb;
  38.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement