为了账号安全,请及时绑定邮箱和手机立即绑定

上传文件reactjs err 400

上传文件reactjs err 400

守候你守候我 2022-06-09 11:21:11
当我尝试上传文件时出现此错误未捕获(承诺中)错误:请求失败,状态码为 404"。我不明白为什么。这是我的一段代码。        import React, {Component}from 'react';    import '../css/formLocation.css';    import axios from 'axios'        class FormLocation extends Component {      constructor(props) {        super(props);        this.state = {           file : null         }      }              handleClick9 = (e) => {      e.preventDefault();      this.setState({step9 : true})      const formData = new FormData();      formData.append('images' , this.state.media)      axios.post('http://localhost:1337/testmedias/', formData)        .then((response) => {          console.log(response);        })      }                  handleChangefile = e => {      this.setState({        media : e.target.files[0]      })      }            render() {         return (           <form>          <div className='form' id='input' >                           <h6 className={this.state.step9? 'true': 'false'}>Ajoutez des images ?</h6>              <input type='file' name='media' className='input' onChange={this.handleChangefile}/>              <br/>              <button onClick={this.handleClick9} >Soumettre</button>          </div>      </form>         );      }    }     export default FormLocation;我正在为 API 使用 Strapi。先感谢您。
查看完整描述

2 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

我不认为你的代码有问题。它给出了 404 的错误,这意味着没有您调用的 API。



查看完整回答
反对 回复 2022-06-09
?
守着一只汪

TA贡献1872条经验 获得超4个赞

尝试这样的事情


import React, {Component}from 'react';

    import '../css/formLocation.css';

    import axios from 'axios'


    class FormLocation extends Component {

      constructor(props) {

        super(props);

        this.state = { 

          file : null

         }

      }



      handleClick9 = (e) => {

      e.preventDefault();

      this.setState({step9 : true})

      const formData = new FormData();

      formData.append('images' , this.state.media)

      axios.post(`'http://localhost:1337/testmedias/'${formData}`)

        .then((response) => {

          console.log(response);

        })

      }  



      handleChangefile = e => {

      this.setState({

        media : e.target.files[0]

      })

      }


      render() { 

        return ( 

          <form>

          <div className='form' id='input' >


              <h6 className={this.state.step9? 'true': 'false'}>Ajoutez des images ?</h6>

              <input type='file' name='media' className='input' onChange={this.handleChangefile}/>

              <br/>

              <button onClick={this.handleClick9} >Soumettre</button>

          </div>

      </form>

         );

      }

    }



export default FormLocation;


查看完整回答
反对 回复 2022-06-09
  • 2 回答
  • 0 关注
  • 273 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号