简体   繁体   English

使用ReactJS和Laravel将请求发送到服务器(文件)

[英]Sending Request to Server (File) Using ReactJS and Laravel

this is my first time creating upload file in reactjs and my server is laravel. 这是我第一次在reactjs中创建上传文件,而我的服务器是laravel。 I have problem regarding uploading files. 我在上传文件时遇到问题。

I try to use the console to detect if is their changes happen when i upload file. 我尝试使用控制台检测上载文件时是否发生了更改。 yes probably the file is show in my console. 是的,该文件可能显示在我的控制台中。

however in my server i use the hasFile of laravel it says that. 但是,在我的服务器上,我使用了laravel的hasFile,它表示。 No file found. 找不到文件。

  1. Why No File found server detect 为什么找不到文件服务器检测到
  2. Why the status is OK 为什么状态正常
  3. Lastly in my console found that i upload file, but in the server detect that no file found. 最后,在我的控制台中发现我上传了文件,但是在服务器中检测到未找到文件。

样本图片

My Controller 我的控制器

public function save_home_content(Request $request)
{

    if($request->hasFile('image'))
    {
        echo 'Have File';
    }
    else
    {
        echo 'No File';
    }


}

My Input 我的输入

<div className="col-md-6" id="file-content">
    <div className="form-group">
        <label htmlFor="exampleFormControlFile1">Image</label>
        <input type="file" 
        name="image"
        onChange={this.handleChangeImage}
        className="form-control-file" 
        accept="image/x-png,image/gif,image/jpeg" 
        id="exampleFormControlFile1"/>
    </div>
</div>

My handle 我的把柄

    handleChangeImage(e){
    this.setState({
        image:e.target.files[0]
    })

    console.log(e.target.files);
}

Axios 爱可信

        axios.post('/api/save_home_content', this.state.image).then(response => {
        console.log(response);
    }).catch(error => (error.response));

Regards.. 问候..

for upload image with react js and laravel you must first of all add this encType="multipart/form-data" to the <Form> componenet after that in axios request do like this and add important thing headers: { 'Content-Type': 'multipart/form-data', }, 对于使用react js和laravel上传图像,您必须首先将此encType="multipart/form-data"<Form>组件中,然后在axios请求中这样做并添加重要的headers: { 'Content-Type': 'multipart/form-data', },

this is result code for upload image with laravel and react js 这是使用laravel和react js上传图像的结果代码

    var formData = new FormData();
    formData.append('image', this.state.image);
    formData.append('name', this.state.name);
    formData.append('family', this.state.family);
    formData.append('body', this.state.body);
    formData.append('email', this.state.email);
    formData.append('phone', this.state.phone);
    formData.append('subject', this.state.subject);

    axios.post('http://localhost:8000/api/contact_us',
        formData,
        {
            headers: {
                'Content-Type': 'multipart/form-data',
            },

        }
        )}

the key to solving your problem is just add headers: { 'Content-Type': 'multipart/form-data', }, to your axios request hope work for you:) 解决问题的关键只是在axios请求中添加标头:{'Content-Type':'multipart / form-data',}希望对您有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM