从JavaScript发送文件时,Symfony请求保持为空

时间:2018-11-19 16:10:22

标签: javascript php reactjs symfony

我正在尝试使用React.js和dropzone包发送文件。 我可以发送文件,但是我的php控制器中的请求不包含该文件。

代码如下:

Javascript:

handleDrop = files => {
  // const uploaders = files.map(file => {
      const formData = new FormData();
      formData.append("file", files[0], 'picture');

      return axios.post("http://localhost:8000/upload", formData, {
          headers: {
              'accept': 'application/json',
              'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
          }})
          .then(response => {
              const data = response.data;
              console.log(data);
          })
  // })
};

render() {
    return (
        <div>
            <NavigationBar>
                displaySearchFields={'none'}
            </NavigationBar>
            <Dropzone onDrop={this.handleDrop}>
                <div>Try dropping some files here, or click to select files to upload.</div>
            </Dropzone>
        </div>
    );
}

Php:

/**
 * @Route("/upload", methods={"POST", "OPTIONS"}, name="upload")
 */
public function uploadImageToDatabase(Request $request)
{
    $data = $request->files->get("file");

    $this->saveImage($data);

    return new JsonResponse($request);
}

public function saveImage($image){
    $fp = fopen("D:\Temp\imageToEncode.png", "w");
    fwrite($fp, $image);
    fclose($fp);
}

有人知道我在做什么错吗?

0 个答案:

没有答案