Node.js 使用multer发送状态和文件
我想用multer中的文件发送数据。从React应用程序到节点服务器 建造师(道具){ 超级(道具) 这给我带来了一个错误,fd是只读的 所以我找了找Node.js 使用multer发送状态和文件,node.js,reactjs,multer,Node.js,Reactjs,Multer,我想用multer中的文件发送数据。从React应用程序到节点服务器 建造师(道具){ 超级(道具) 这给我带来了一个错误,fd是只读的 所以我找了找 fd.append('file',e.target.files[0],e.target.files[0].name) fd.append('state',this.state) console.log(fd) axios.post(`/api/${this.state.area}`,fd)
fd.append('file',e.target.files[0],e.target.files[0].name)
fd.append('state',this.state)
console.log(fd)
axios.post(`/api/${this.state.area}`,fd)
.then((res)=>{
this.setState({"isPassed":true})
})
.catch((e)=>{
if(e){
console.log(e.response)
this.setState({'isProcessing':false,
'errorLine':e.response.data.error})
}
})
但当我在服务器上打印时
router.post('/somethin',upload,userVerifyMiddleWare,(reqs,resp)=>{
console.log(reqs.body.state.data)
})
它将未定义的显示为输出。但我将获得所需输出的reqs.files
const upload =multer({storage: multer.diskStorage({
destination: function (req, file, callback) { callback(null, './temp/');},
filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now()+'.csv');}})
}).single('file');
是multer对象。错误fd为只读是因为您将formData变量fd声明为const,只需将其改为let,它应该可以解决问题。我找到了一种方法。实际上我不知道发送对象的方法,但要解决问题,我们可以
fd.append('file',e.target.files[0],e.target.files[0].name)
fd.append('state-value-1',this.state.value1)
fd.append('state-value-2',this.state.value2)
在服务器上,我们可以
console.log(req.body) // to get all non file values
console.log(req.file) // for file
谢谢。但即使更改了,我也无法发送带有文件的状态。请提供更多帮助。
console.log(req.body) // to get all non file values
console.log(req.file) // for file