将用户输入文件vuejs发送到symfonyphp时出现问题



enter code here在此输入图像描述我在将图片文件从前端(vue-js(发送到后端(php-symfony(时遇到问题。我已经制作了一个表单,用户可以在其中输入图像文件,现在我想将图像文件发送到后端并保存在那里。该文件是用一个fetch多部分表单发送的,问题是php不是以文件的形式读取,而是以字符串的形式读取。在此处输入图像描述

在此处输入图像描述

---vue.js---
catchImg(event) {
this.imgFile = event.target.files[0]
this.form.naamImg = event.target.files[0].name
//console.log(this.form.imgFile)
//console.log(this.form.naamImg);
let formData = new FormData()
formData.append("file", this.imgFile)
fetch('https://localhost:8000/savePicture', {
headers: {
'Content-Type': 'multipart/form-data'
},
body: formData,
method: "POST"
})
.then((response) => {
return response.json();
})
},
---symfony---
/**
* @Route("/savePicture", name="savePicture")
*/
public function savePicture(Request $request){
$data = $request->getContent();
dd($data);
//dd($_FILES);
//$destination = $this->getParameter('kernel.project_dir').'/public/uploads';
//$data->move($destination);
$response = new JsonResponse(
[
'picture saved' => 'ok',
],
JsonResponse::HTTP_CREATED
);
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
}

文件可以通过$request->files->get()方法访问,因为RequestFileBag,类似于ParameterBag-在这个线程中有更多

最新更新