1

I am making angular application with image upload option which has the,

Html :

<label class="hoverable" for="fileInput">
  <img [src]="url ? url : avatarImage"> 
  <div class="hover-text">Choose file</div>
  <div class="background"></div>
</label>
<br/>
<input id="fileInput" type='file' (change)="onSelectFile($event)">
<button *ngIf="url" (click)="delete()" >delete</button>


<img (click)="uploadPersonaImage($event)" class="avatar-images" src="https://www.w3schools.com/howto/img_avatar.png">

<img (click)="uploadPersonaImage($event)" class="avatar-images" src="https://www.w3schools.com/howto/img_avatar2.png">

Here what i am having is if the user clicks over the image he can select and update whatever image he has in local.

Same way if the user was not interested to update the profile image but interested to select any of the avatar image as per his/her wish which i have given like,

 <img (click)="uploadPersonaImage($event)" class="avatar-images" src="https://www.w3schools.com/howto/img_avatar.png">

<img (click)="uploadPersonaImage($event)" class="avatar-images" src="https://www.w3schools.com/howto/img_avatar2.png">

And in ts made something like this,

uploadPersonaImage(e) {
  this.url = e.target.src;
}

So on the click function the src that comes from the event.target was set to this.url..

But i need to convert it as file.. Because i need to send it as file to the service call so i need to update the avatar image.

So please help me to convert the avatar image selected/clicked by the user to the file/formdata so that it can be sent to the service as file format and can be updated as user selected image..

Example: https://stackblitz.com/edit/angular-file-upload-preview-85v9bg

2
  • angular change event doesn't work on input type file Commented Jan 10, 2019 at 12:49
  • Unable to get you but I am not at all going to use input type file here (only to upload from local machine of user).. Commented Jan 10, 2019 at 12:51

1 Answer 1

2

You can use FormData to attach the read file and send to the API.

 onSelectFile(event) {
    if (event.target.files && event.target.files[0]) {

      this.uploadToServer(event.target.files[0]);
      ... rest of the code
  }

  uploadToServer(file) {
    let formData: FormData = new FormData();
    formData.append('fileName', file);
    // call your api service to send it to server, send formData
  }

EDIT:

Try this out if you have no option to touch onSelectFile() or trigger a different function when you upload the file.

_url = ''
set url(val) {
  this._url = val;
  if (val) {
    this.dataURLtoFile(val);
  }
}

get url() {
    return this._url;
}
uploadedImage: File ;

dataURLtoFile(dataurl) {
    const arr = dataurl.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const imageExtension = mime.split('/')[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    this.uploadedImage = new File([u8arr], `uploaded.${imageExtension}`);
  }

On your API call, maybe when you click on a button,

uploadPersonaImage(e) {
  // this.apiService.someMethod(this.uploadedImage);
}

If you want to trigger the API call just when you upload the image, add the code of dataURLtoFile() to uploadPersonaImage() and call uploadPersonaImage() from url setter

Clarification

Do you understand what does event.target.src mean (considering e as event)?

  1. Here event means the click/change event you triggered when you clicked onto upload photo.

  2. event.target means the DOM element on which the event took place.

  3. event.target.src will give you the src attribute value of the DOM element on which you triggered the change event.

Now, you say won't it work? No, it won't because the element which you clicked is an HTMLInputElement but the src resides under the image in under the label tag. And how are you intending to call uploadPersonaImage()? what calls your method? You haven't answered that even after asking so many times.

In my last edit, I have added code under the setter of the url which will convert the dataUrlFile to an actual File, It completely depends on your server how you want to store the file. As a file or as a dataUrl? If you want to send it as a file then follow the conversions I added in the answer if you want to save as dataUrl then directly save the content of this.url on your API call.

Sign up to request clarification or add additional context in comments.

18 Comments

Can i have example from you?? Should i need to replace this uploadPersonaImage(e) { this.url = e.target.src; } with what you have given??
@HelloWorld yes, add the content of uploadToServer() under uploadPersonaImage(), call it from onSelectFile as I have called uploadToServer()
I am making it clear that i am not at all talking anything with onSelectFile(event) only thing is need help of how to convert the image in uploadPersonaImage(e) to file..
@HelloWorld you don't need a File, all you need is convert to a BLOB, and send a FormData (as xyz did) to your server
I'm not sure if you guys are talking about the same things...I quickly implemented xyz solution : stackblitz.com/edit/angular-file-upload-preview-squj4f
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.