React Laravel上传多张图片

时间:2019-09-30 14:24:56

标签: reactjs laravel foreach

我正在尝试以一种形式上传多个图像,并且文件以对象的形式发送到Laravel控制器,但没有上传到那里。 这是我的代码。

客户端(ReactJS)

this.state = {
         selectedFile:      [],
    }

图片更改JS

handleImageChange = (e) =>{
    this.setState({
        selectedFile: e.target.files,
      })
}

提交处理程序

 submitHandler = (e) =>{
    e.preventDefault()
    const datafile = new FormData()
    datafile.append('file', this.state.selectedFile)
    this.props.hoprofilecreate(datafile)
}

服务器端(Laravel)

public function hoProfileCreate(Request $request){
$hoprofile                  =   new ProfileHospital;    
 if ($request->file) {
        $names = [];
        foreach ($request->file as $file) {        
            $name = time() . '.' . explode('/', explode(':', substr($file, 0, strpos($file, ';')))[1])[1];
            request()->file->move(storage_path('app/public/doctorimages/'), $name);
            array_push($names, $name);
        }
        $hoprofile->pics = $names;
    }

}
$hoprofile-> save();
$datax = $request->all(); 
    $response = ['success'=>true, 'data'=>'Dr Profile uplaoded in Contorller. Check', 'datax' => $datax, 'name'=> $names];
    return response()->json($response, 201);

iam进入控制台的数据是

enter image description here

0 个答案:

没有答案