试图准备一个带有fs的文件,通过php上传到服务器



我当前有一个本地文件路径

var localFile = "/Desktop/here/myfile.jpg";
const buffer = fs.readFileSync(localFile);
const fileName = 'thumb.jpg';

然后我尝试将其添加到FormData对象中,如下所示:

formData.append('file', buffer, {
contentType: 'text/plain',
name: 'file',
filename: fileName,
});

然后我得到错误:

Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.

我对fs流有点困惑。readFileSync不应该创建blob吗?

我最终是这样做的:

fs.readFile(`${localFile}`, (error, data) => {
if(error) {
throw error;
}
formData.append('file', data);
});

最新更新