How to get a file or blob from an object URL?

BrianFreud picture BrianFreud · Aug 9, 2012 · Viewed 159.9k times · Source

I am allowing the user to load images into a page via drag&drop and other methods. When an image is dropped, I'm using URL.createObjectURL to convert to an object URL to display the image. I am not revoking the url, as I do reuse it.

So, when it comes time to create a FormData object so I can allow them to upload a form with one of those images in it, is there some way I can then reverse that Object URL back into a Blob or File so I can then append it to a FormData object?

Answer

user993683 picture user993683 · Sep 19, 2018

Modern solution:

let blob = await fetch(url).then(r => r.blob());

The url can be an object url or a normal url.