How to upload files in FormData as Stream #1638
-
Hello undici team. I am planning to create a program to upload files on Node.js using undici. The following code will send the file data as the text import { request, FormData, File } from 'undici'
import fs from 'fs'
let formData = new FormData()
// formData.append('file', new File([fs.readFileSync('test.mp4')], 'test.mp4')) // no stream
formData.append('file', fs.createReadStream('test.mp4'))
let res = await request('http://localhost:3000/', { method: 'POST', body: formData })
console.log(await res.body.text()) Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Answered by
jimmywarting
Sep 6, 2022
Replies: 1 comment 1 reply
-
for this end we need to be able to get Blob/Files backed up by the filesystem. anyhow... for now i think your best solution is to either
import { fileFromSync } from 'fetch-blob/from.js'
// will only stat the file for size & last modified date
const file = fileFromSync('test.mp4', 'video/mp4')
// this will not actually read anything into memory (yet) only when it needs to upload/serialize the FormData
// will it actually start to read the file
formData.append('file', file) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ugonight
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
for this end we need to be able to get Blob/Files backed up by the filesystem.
there is a feature request for this here: nodejs/node#37340 (give it a 👍)
anyhow... for now i think your best solution is to either
new File
(like your uncommented line) (not recommended for large files)