Yui
Yui3y ago

upload files formdata

How can I upload files with multipart/form-data using fetch?
17 Replies
javi
javi3y ago
This is what you’re looking for: https://muffinman.io/blog/uploading-files-using-fetch-multipart-form-data/ You can always type the same in google, you’ll get an answer 100%
Yui
Yui3y ago
I tried some stuff and did not get it to work so I asked. Might come back.
javi
javi3y ago
Alright sure, but with such easy queries at least try lol. Mdn is a great place to start
Yui
Yui3y ago
Alright so got it to work.
Yui
Yui3y ago
What if I want to send more files at once though. 🤔 should be possible somehow
Andreu Botella (they/them)
more files in the same form field? what does the server expect?
Yui
Yui3y ago
apparently I need to make it json somehow or something? I do not really understand this. https://discord.com/developers/docs/reference#uploading-files
Discord Developer Portal
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.
Andreu Botella (they/them)
oh, then I guess
formData.append("files", file1);
formData.append("files", file2);
formData.append("files", file1);
formData.append("files", file2);
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Andreu Botella (they/them)
filesystem based blobs are not supported, but you can read the files into memory that's only an issue if the files are huge
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Yui
Yui3y ago
For my use case it will just be long strings converted to Blobs so I should be fine with that.
Andreu Botella (they/them)
if the server is expecting files, they might need a filename and a mime type but if you're building the blobs yourself, ok
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Andreu Botella (they/them)
well, if you don't specify the mime type it gets set to the empty string because multipart/form-data file fields must have a mime type header but anyway
Yui
Yui3y ago
got it to work. Thanks for the help 👍
Yui
Yui3y ago