Skip to content

Instantly share code, notes, and snippets.

@AshikNesin
Last active December 19, 2023 06:08
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save AshikNesin/ca4ad1ff1d24c26cb228a3fb5c72e0d5 to your computer and use it in GitHub Desktop.
Save AshikNesin/ca4ad1ff1d24c26cb228a3fb5c72e0d5 to your computer and use it in GitHub Desktop.
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
Copy link

thank you for sharing!

@mik4ik
Copy link

mik4ik commented Jun 16, 2020

Thank you so much!
It helped me :)

@AshikNesin
Copy link
Author

Glad it was helpful @mik4ik & @pierceh89 :)

@nfahrni
Copy link

nfahrni commented Dec 4, 2020

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

@AshikNesin
Copy link
Author

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

@rnidea2code
Copy link

TypeError: Network request failed

@cgrewon
Copy link

cgrewon commented Apr 22, 2021

TypeError: Network request failed

Same for me also

@RowenaRavenclawWithExtraClaws

TypeError: Network request failed

Same here :(

@PrafullKumarB
Copy link

Thanks for sharing

@zhcwang
Copy link

zhcwang commented Oct 4, 2022

Genius, thanks.

@jxxe
Copy link

jxxe commented Jul 22, 2023

Disgusting. Thanks.

@hcallejas
Copy link

hcallejas commented Oct 19, 2023

TypeError: Network request failed

you need to create the base64 with all its structure ""data:mimeType;base64,base64data":

"data;application/pdf;base64,afdnxhfuiesfslg........"

@SolankiYogesh
Copy link

thank you bro you saved my me

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