Skip to content

Instantly share code, notes, and snippets.

@dermatologist
Forked from AshikNesin/base64-form-data.js
Created December 24, 2019 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermatologist/56b3d88f0dd36912e04b74eb2ef1d026 to your computer and use it in GitHub Desktop.
Save dermatologist/56b3d88f0dd36912e04b74eb2ef1d026 to your computer and use it in GitHub Desktop.
Base64 image to multipart/form-data
const base64 = 'data:image/png;base65,....' // 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment