Created
February 26, 2021 13:03
base64 file to form/data - nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Awesome, my error was just not sending the filename. Thanks!