【发布时间】:2021-07-13 22:04:34
【问题描述】:
我正在制作一个使用我编写的 API 的应用程序,事情是,我需要客户端将图像发送到 API,然后将其保存在服务器端,我使用文件输入成功发送了图像,并且以下脚本:
const upload = _ => {
let form = new FormData();
form.append("file", document.getElementById("my-file-selector").files[0])
fetch('http://localhost:3377/me/uploadPfp', {
method: 'POST',
headers: {
"Authorization": "<%= locals.user.token %>",
"Content-Type": "application/x-www-form-urlencoded"
},
body: form,
}).then(
response => response.json()
).then(
success => console.log(success)
).catch(
error => console.log(error)
);
};
服务器端它似乎工作,但我无法使用fs.writeFile() 保存它,它返回此错误:
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.
但是当我 console.log 接收到的文件时,会发生这种情况: Receipt Image
【问题讨论】:
标签: javascript express file api-design