Javascript 上传文件错误400

Javascript 上传文件错误400,javascript,reactjs,file-upload,Javascript,Reactjs,File Upload,当我试图上传文件时,我有这个错误 未捕获(承诺中)错误:请求失败,状态代码为404” 我不明白为什么。这是我的密码 import React,{Component}来自'React'; 导入“../css/formLocation.css”; 从“axios”导入axios 类FormLocation扩展组件{ 建造师(道具){ 超级(道具); this.state={ 文件:空 } } handleClick9=(e)=>{ e、 预防默认值(); this.setState({step9:

当我试图上传文件时,我有这个错误

未捕获(承诺中)错误:请求失败,状态代码为404”

我不明白为什么。这是我的密码

import React,{Component}来自'React';
导入“../css/formLocation.css”;
从“axios”导入axios
类FormLocation扩展组件{
建造师(道具){
超级(道具);
this.state={
文件:空
}
}
handleClick9=(e)=>{
e、 预防默认值();
this.setState({step9:true})
const formData=new formData();
formData.append('images',this.state.media)
轴心柱http://localhost:1337/testmedias/,formData)
。然后((响应)=>{
控制台日志(响应);
})
}  
handleChangefile=e=>{
这是我的国家({
媒体:e.target.files[0]
})
}
render(){
报税表(
阿尤兹图片?

苏梅特 ); } } 导出默认表单位置;
我认为您的代码没有问题。它给出的错误为404,这意味着没有您调用的API。

尝试类似的方法

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;
import React,{Component}来自'React';
导入“../css/formLocation.css”;
从“axios”导入axios
类FormLocation扩展组件{
建造师(道具){
超级(道具);
this.state={
文件:空
}
}
handleClick9=(e)=>{
e、 预防默认值();
this.setState({step9:true})
const formData=new formData();
formData.append('images',this.state.media)
axios.post(`http://localhost:1337/testmedias/“${formData}`”)
。然后((响应)=>{
控制台日志(响应);
})
}  
handleChangefile=e=>{
这是我的国家({
媒体:e.target.files[0]
})
}
render(){
报税表(
阿尤兹图片?

苏梅特 ); } } 导出默认表单位置;
是的,有一个API。这是我的API[{“id”:1,“创建于”:“2020-03-30T17:40:24.948Z”,“更新于”:“2020-03-30T17:40:24.948Z”,“图像”:[{“id”:2,“名称”:“Capture d'cran(36.png)”,“哈希”:“5bb71f706ce443078eed99a46b1d7fb4”,“sha256”:“GwyVbHbHwW7A8GioJfpckgi5PNY0V-d0V-d0tQ0tpe”,“image”;“大小”:“url”:“http.png”;“http”;“http”;“http”;“http:.png”;“size”;“http”;“http:.401/上传/5bb71f706ce443078eed99a46b1d7fb4.png,“提供者”:“本地”,“提供者元数据”:null,“创建地点”:“2020-03-30T17:40:24.999Z”,“更新地点”:“2020-03-30T17:40:24.999Z”}