Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image url to file() object using js

For a registration module in my vue app I let users upload images in a form. I upload these images to my storage and save the download url of this photo in the registration. When editing a registration I need to get the photo's out of the storage which is simple since I got the url. But I need it to be a file() object. I have found ways to turn it into a blob but it needs to be a file. How can I do this?

like image 255
Travinns Avatar asked Mar 04 '19 12:03

Travinns


1 Answers

It can be done by requesting a blob and generating a File object. It is necessary to specify the MIME type of the blob.

const urlToObject= async()=> {
  const response = await fetch(image);
  // here image is url/location of image
  const blob = await response.blob();
  const file = new File([blob], 'image.jpg', {type: blob.type});
  console.log(file);
}
like image 81
Deepak Kumrawat Avatar answered Sep 19 '22 15:09

Deepak Kumrawat