gpt4 book ai didi

javascript - 如何在nodejs中接收上传的文件

转载 作者:行者123 更新时间:2023-12-01 01:15:28 26 4
gpt4 key购买 nike

我有一个 redux-from 提交,控制台日志如下所示

{
id: "x",
parentId: "b",
editing: true,
logo: FileList,
}

Logo FileList包含我上传的文件名和文件,我认为

0: File(19355) {name: "11964821.jpeg", lastModified: ......

使用fetch发送到nodejs,图像包含我的文件和上面的数据

function Update(image) {

const requestOptions = {
method: 'POST',
headers: { ...authHeader(), 'Content-Type': 'application/x-www-form-urlencoded' },
body: JSON.stringify(image)
};
return fetch(`${apiUrl}/API`, requestOptions).then(handleResponse)
.then( data => { return data; });
}

但是这个数据没有接收到nodejs,因为尝试了re.file等..它显示未定义

app.post('/image', upload.single('logo'), function (req, res, next) {

})

<小时/>包括 表单数据

  const onSubmit = (image, dispatch)=>{

var formData = new FormData();
formData.append('logo',image.logo[0]);

var imageNew = {...image,formData};

dispatch(imageActions.companyProfileLogoUpdate(imageNew)) ;
}

现在日志

formData: FormData {}
id: "5c66478b0814720a4365fe72"
logo: FileList {0: File(19355), length: 1}
parentId: "5c66478b0814720a4365fe71"

最佳答案

let formData = new FormData();
formData.append('logo' , {{your image file}}

// upload file as
let result = await sendServerRequestForForm(url,formData);


function sendServerRequestForForm(url,data) {
var promise = new Promise(function(resolve) {
resolve($.ajax({
url,
method : 'POST',
data,
processData: false,
contentType: false,
success: function(data) {
return data
},
error: function (err) {
try{
let responseStatus = err.responseJSON
// watch your response
}catch (e) {
console.log('Field to get response');
}
}
}));
});
return promise

}

关于javascript - 如何在nodejs中接收上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54802005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com