Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Base64 image to multipart/form-data
const base64 = 'data:image/png;base64,....' // Place your base64 url here.
fetch(base64)
.then(res => res.blob())
.then(blob => {
const fd = new FormData();
const file = new File([blob], "filename.jpeg");
fd.append('image', file)
// Let's upload the file
// Don't set contentType manually → https://github.com/github/fetch/issues/505#issuecomment-293064470
const API_URL = 'https://example.com/add_image'
fetch(API_URL, {method: 'POST', body: fd)
.then(res => res.json())
.then(res => console.log(res))
@pierceh89

This comment has been minimized.

Copy link

@pierceh89 pierceh89 commented Jun 10, 2020

thank you for sharing!

@mik4ik

This comment has been minimized.

Copy link

@mik4ik mik4ik commented Jun 16, 2020

Thank you so much!
It helped me :)

@AshikNesin

This comment has been minimized.

Copy link
Owner Author

@AshikNesin AshikNesin commented Jun 16, 2020

Glad it was helpful @mik4ik & @pierceh89 :)

@nfahrni

This comment has been minimized.

Copy link

@nfahrni nfahrni commented Dec 4, 2020

Nice it helped! const base64 = 'data:image/png;base65,....' your string is funny as you wrote base65 :)

@AshikNesin

This comment has been minimized.

Copy link
Owner Author

@AshikNesin AshikNesin commented Dec 5, 2020

@nfahrni my bad, didn't notice it before. Fixed it now, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment