我在将 blob 附加到 FormData 时遇到了一个奇怪的问题。根据文档(https://developer.mozilla.org/en-US/docs/Web/API/FormData/append),追加函数可以是 String 或 Blob。我的代码是这样的:
const blobFromSync = (...args) =>
import('node-fetch').then(({ blobFromSync }) => blobFromSync(...args));
let file_location = '/path/to/video/file.mp4';
const file = await blobFromSync(file_location);
const chunkSize = 40000;
for (let start = 0; start < file.size; start += chunkSize) {
const chunk = file.slice(start, start + chunkSize + 1)
const form = new FormData();
form.append('file', chunk, 'an-id');
}
控制台记录该块显示它是一个 Blob,但它抛出此错误:
TypeError: source.on is not a function
at DelayedStream.create (/Users/xxxxxxxxx/Development/terminal-backend/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at CombinedStream.append (/Users/xxxxxxx/Development/terminal-backend/node_modules/combined-stream/lib/combined_stream.js:45:37)
at FormData.append (/Users/xxxxxxxxx/Development/terminal-backend/node_modules/form-data/lib/form_data.js:75:3)
at /Users/xxxxxxxxxn/Development/terminal-backend/src/server.js:90:18
我在这里忽略了什么?
这可行,但还有一件事是不要忘记在 FormData 选项中包含文件名。
这里的数据是我从supabase得到的
const data = await this.uploadService.downloadFileFromSupabase(audiofileurl);
从'form-data'导入*作为FormData;
从 'node:buffer' 导入 { Buffer };
const formData = new FormData();
const buf = await data.arrayBuffer(); // here data is Blob - Blob { size: 97728, type: 'audio/wav' }
// convert blob to buffer as formdata doesnot accept it directly.
formData.append("file", Buffer.from(buf),{
filename:"audio.wav",
});
const apiResult = await axios.post(ENDPOINT,formData);