Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.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
Javascript Express multer在req.file上返回未定义_Javascript_Reactjs_Express_Multer - Fatal编程技术网

Javascript Express multer在req.file上返回未定义

Javascript Express multer在req.file上返回未定义,javascript,reactjs,express,multer,Javascript,Reactjs,Express,Multer,我试图将一个文件传递给节点,而express不会将其存储在req.body中,因此我使用multer中间件,但每当我记录req.file时,我都会得到未定义的文件。我不确定我做错了什么 react/index.js fileSelectHandler = e => { axios.post("/api/upload", { profilePic: e.target.files[0] }); this.setState({ profilePic: e.target.files[0]

我试图将一个文件传递给节点,而express不会将其存储在req.body中,因此我使用multer中间件,但每当我记录req.file时,我都会得到未定义的文件。我不确定我做错了什么

react/index.js

fileSelectHandler = e => {
  axios.post("/api/upload", { profilePic: e.target.files[0] });
  this.setState({ profilePic: e.target.files[0] });
};

render() {
  return (
    <input
      onChange={event => this.fileSelectHandler(event)}
      name="profilePic"
      type="file"
    />
  );
}
您可以改为使用上载文件

fileSelectHandler = e => {
  const formData = new FormData();
  const profilePic = e.target.files[0];

  formData.append("profilePic", profilePic);

  axios.post("/api/upload", formData);
  this.setState({ profilePic });
};
您可以改为使用上载文件

fileSelectHandler = e => {
  const formData = new FormData();
  const profilePic = e.target.files[0];

  formData.append("profilePic", profilePic);

  axios.post("/api/upload", formData);
  this.setState({ profilePic });
};

我认为你需要上传你的文件作为一个表单数据类型,如下所示

fileSelectHandler = e => {
const formData = new FormData();
formData.append(‘profilePic’,e.target.files[0]);

const config = {
    headers:{
        ‘content-type’:’multipart/form-data’
        }
    }
axios.post("/api/upload", formData, config);
this.setState({ profilePic: e.target.files[0] });
};

希望能有所帮助。

我认为您需要将文件上传为表单数据类型,如下所示

fileSelectHandler = e => {
const formData = new FormData();
formData.append(‘profilePic’,e.target.files[0]);

const config = {
    headers:{
        ‘content-type’:’multipart/form-data’
        }
    }
axios.post("/api/upload", formData, config);
this.setState({ profilePic: e.target.files[0] });
};

希望能有所帮助。

多文件输入的另一个示例。在Express JS上测试

function uploadImages(post) {
    return new Promise((resolve, reject) => {
        const formData = new FormData();
        for( var i = 0; i < post.length; i++ ){
            formData.append('multifiles', post[i]);
        }
        let jsonPost = {
            method: POST_REQUEST,
            url: UPLOAD_FILE_URL,
            data: formData,
            headers: {
                'content-type': 'multipart/form-data',
            }
        };
        fetchData(jsonPost)
            .then(res => {
                console.log("RESULT uploadImages: ", res.data);
                resolve(res.data);
            })
            .catch(reject);
    });
}
函数上传图像(post){
返回新承诺((解决、拒绝)=>{
const formData=new formData();
对于(变量i=0;i{
log(“结果上传图像:”,res.data);
解析(res.data);
})
.捕获(拒绝);
});
}

多文件输入的另一个示例。在Express JS上测试

function uploadImages(post) {
    return new Promise((resolve, reject) => {
        const formData = new FormData();
        for( var i = 0; i < post.length; i++ ){
            formData.append('multifiles', post[i]);
        }
        let jsonPost = {
            method: POST_REQUEST,
            url: UPLOAD_FILE_URL,
            data: formData,
            headers: {
                'content-type': 'multipart/form-data',
            }
        };
        fetchData(jsonPost)
            .then(res => {
                console.log("RESULT uploadImages: ", res.data);
                resolve(res.data);
            })
            .catch(reject);
    });
}
函数上传图像(post){
返回新承诺((解决、拒绝)=>{
const formData=new formData();
对于(变量i=0;i{
log(“结果上传图像:”,res.data);
解析(res.data);
})
.捕获(拒绝);
});
}