最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Upload File Api请求在邮递员中有效,但在nodejs中不起作用,并且对MERN有反应

运维笔记admin16浏览0评论

Upload File Api请求在邮递员中有效,但在nodejs中不起作用,并且对MERN有反应

Upload File Api请求在邮递员中有效,但在nodejs中不起作用,并且对MERN有反应

我正在制作文件上传和媒体API,在Postman中可以正常工作,但在代码中不行

反应代码:

campaignChangedHandler = (event) => {
   this.setState(
        {
            campaignFile: document.getElementById('campaignFile').files[0]
        })
};

onSubmit = async(e) => {
   let formData = new FormData();
    formData.append('campaignFile', this.state.campaignFile);

     const headers = {
        'Authorization': `Bearer ${token}`
     };

      await axios.post('http://localhost:3001/upload/', {formData}, {headers}).then(resp => {
         console.log(resp)
      })
};


render() {
  return (
    <form onSubmit={this.onSubmit}>
        <input value={this.state.data} error={errors.data}
            onChange={this.campaignChangedHandler}
            name={"campaignFile"}
            id="campaignFile"
            type="file"/>
    </form>
  )
}

NODE JS代码:

const fileUpload = require('express-fileupload');
app.use(fileUpload());

app.post('/upload', function (req, res) {
   console.log(req.files);
}

[我总是在Nodejs控制台中得到不确定的信息,我尝试过使用多方,multer和busboy,但是得到了相同的结果,但是在邮递员中,我得到了这个:

response from postman

回答如下:

尝试一下:

constructor(props) {
    super(props);
      this.state = {
        campaignFile: null
      }

  }



 onChangeHandler=event=>{
    this.setState({
      campaignFile: event.target.files[0],
    })
  }



 onSubmit = async(e) => {
   let formData = new FormData();
   formData.append('campaignFile', this.state.selectedFile);

     const headers = {
        'Content-Type': 'multipart/form-data',
        'Authorization': `Bearer ${token}`
     };

      await axios.post('http://localhost:3001/upload/',formData,headers).then(resp => {
         console.log(resp)
      })
};


render() {
  return (
    <form onSubmit={this.onSubmit}>
        <input type="file" name="campaignFile" onChange={this.onChangeHandler}/>
    </form>
  )
}
发布评论

评论列表(0)

  1. 暂无评论