From b000d5119a79eef861205de703642ac049354b94 Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Tue, 2 Mar 2021 16:51:16 +0100 Subject: [PATCH] Return correct file --- .../edit-image-modal/edit-image-modal.component.ts | 10 ++++------ projects/common/src/fm/services/image.service.ts | 13 ++++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/projects/common/src/fm/components/edit-image-modal/edit-image-modal.component.ts b/projects/common/src/fm/components/edit-image-modal/edit-image-modal.component.ts index 5e9934c..22b35f1 100644 --- a/projects/common/src/fm/components/edit-image-modal/edit-image-modal.component.ts +++ b/projects/common/src/fm/components/edit-image-modal/edit-image-modal.component.ts @@ -59,15 +59,13 @@ export class EditImageModalComponent implements OnInit { save() { if(this.croppedImage) { - if(this.saveImage) { - var body = this.croppedImage.substr(23); - this.imageService.putImage(this.endpointUrl,this.imageService.b64toBlob(body,"image/jpeg")).subscribe(() => { + var blob = this.imageService.dataUrltoBlob(this.croppedImage); + if(this.saveImage) { + this.imageService.putImage(this.endpointUrl,blob).subscribe(() => { this.changed.emit({}); }); } else { - var selectedImage = (this.imageChangedEvent.target.files && this.imageChangedEvent.target.files[0]) ? - this.imageChangedEvent.target.files[0] : null; - this.changed.emit({ "croppedImage": this.croppedImage, "selectedImage": selectedImage } ); + this.changed.emit({ "croppedImage": this.imageService.blobToFile(blob,"croppedimage.jpg")} ); } this.modalService.dismissAll("Save"); } diff --git a/projects/common/src/fm/services/image.service.ts b/projects/common/src/fm/services/image.service.ts index 24b7a96..f425c73 100644 --- a/projects/common/src/fm/services/image.service.ts +++ b/projects/common/src/fm/services/image.service.ts @@ -21,9 +21,12 @@ export class ImageService { return this.httpClient.put(endpoint,formData); } - b64toBlob(b64Data:string, contentType?:string):Blob { + dataUrltoBlob(dataURI:string):Blob { + var mime = dataURI.split( ';base64,')[0].split(':')[1]; + var byteCharacters = atob(dataURI.split( ';base64,')[1]); + + const sliceSize = 512; - const byteCharacters = atob(b64Data); const byteArrays = []; for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) { @@ -38,7 +41,11 @@ export class ImageService { byteArrays.push(byteArray); } - const blob = new Blob(byteArrays, {type: contentType}); + const blob = new Blob(byteArrays, {type: mime}); return blob; } + + blobToFile(blob:Blob, filename:string):File { + return new File([blob],filename); + } }