Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将包含某些数据的文件上载到node.js服务器_Node.js_Reactjs - Fatal编程技术网

将包含某些数据的文件上载到node.js服务器

将包含某些数据的文件上载到node.js服务器,node.js,reactjs,Node.js,Reactjs,我试图在同一个请求中发送数据并将图像上载到后端(Node.js),但当我尝试上载图像时,发现其他属性在后端未定义 const Imgdata = new FormData() Imgdata.append('file', this.state.choiceImgUrl) axios.post("http://localhost:8080/api/choices/img", Imgdata, { headers: { 'Content-type': 'appl

我试图在同一个请求中发送数据并将图像上载到后端(Node.js),但当我尝试上载图像时,发现其他属性在后端未定义

const Imgdata = new FormData()
Imgdata.append('file', this.state.choiceImgUrl)

  axios.post("http://localhost:8080/api/choices/img", Imgdata, { 
     headers: { 'Content-type': 'application/json' },
     data:{ 
        id:this.state.id,
        username:this.state.username
     }
  })
  .then(res => {
     if(res.data.success){
        this.setState({ successUpload:true })
     }
     else {
        alert('error in upload data')
        this.setState({ Uploadloading:false })

     }
  })
id
username
属性在后端没有定义,如果我删除
Imgdata
,所有数据都会成功传输,但没有文件


如何将文件连同一些数据一起上载到后端?

在frontendserver之间发送数据可以采用多种格式-在这种情况下,我建议使用[Form](因为文件最终是二进制代码)

您的头文件还应该包括(而不是application.json)-

当您将整个结构打包到一个变量中时,只需将其作为参数传递给函数即可

randomService.uploadData(formData)

但是也应该构造后端,以接收特定的值

我正在拍摄当前接受应用程序/json的服务器-因此我将您循环到另一个线程-

Content-Type: multipart/form-data; boundary=something
randomService.uploadData(formData)