Skip to content

Instantly share code, notes, and snippets.

@emindeniz99
Created February 26, 2021 13:03
Show Gist options
  • Save emindeniz99/0b415de896f5c335d253d870116d798f to your computer and use it in GitHub Desktop.
Save emindeniz99/0b415de896f5c335d253d870116d798f to your computer and use it in GitHub Desktop.
base64 file to form/data - nodejs
import * as FormData from "form-data";
base64: string, // it should start with "iVBORw0KGgoA...." instead of "data:image/png;base64,"
fileName: string // it should include file name and extension, like "saype.jpg" instead of "saype"
var formdata = new FormData();
// base64 to buffer, https://stackoverflow.com/questions/37608249/convert-base64-image-to-a-file-in-node-js
let bf = Buffer.from(base64, "base64");
// buffer to form/data, https://stackoverflow.com/questions/43913650/how-to-send-a-buffer-in-form-data-to-signserver
formdata.append("file", bf, fileName);
/*
I tested it with .png, .docx, .pdf, it works
I have base64 data of a file at nodejs server,
I need to upload to discord with webhook, it accepts form/data
when i pass { contenttype:"..pdf", filename:"....pdf" } to formdata.append(), It failed
I dont know why it occured. maybe discord doesnt support contenttype at form/data
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment