2017-06-10 33 views
0

嗨我试图上传一个图像与reactjs,当我从nodejs上载console.log中的图像。我得到一个空数组 任何建议上传图片reactjs/nodejs错误

组件reactjs

export default class Categoria extends Component { 
    constructor(props) { 
     super(props); 
     this.state= {} 
     this.handleSubmit=this.handleSubmit.bind(this) 
    } 

    render(){ 
     return (
      <form onSubmit={this.handleSubmit.bind(this)} encType="multipart/form-data"> 
       <input type="file" onClick={this.handleImageChange.bind(this)} name="photo" /> 
       <button type="submit">upload picture </button> 
      </form>     
     ) 
    } 
    handleImageChange(e) { 
     this.setState({photo: e.target.files[0]});    
    } 
    handleSubmit(e) { 
     e.preventDefault() 
     axios.post("x/photos", this.state.photo) 
     .then((response) =>{ 
      console.log(response.data) 
     }) 
     .catch((err)=>{ 
      console.log(err) 
     }) 
    } 
} 



// router nodejs 
router.post('/x/photos', function(req, res){ 
    console.log(req.body.photo) 
} 
+0

你在axios中得到什么回应? –

+0

我收到了一条好消息,问题是reactjs。为什么用html中的表单测试并且工作 –

回答

1

我不认为你会得到的图像像req.body.photo。使用https://www.npmjs.com/package/multer上传文件

+0

嗨,谢谢,路由器更新router.post('/ x/photos',upload.single('photo'),function(req,res){ \t console.log (req.file)}但控制台日志,显示未定义,我想是的,问题在于反应 –