繁体   English   中英

将用户输入文件 vue js 发送到 symfony php 的问题

[英]problem sending user input file vue js to symfony php

enter code here输入图像描述我在将图片文件从前端(vue js)发送到后端(php symfony)时遇到问题。 我已经制作了一个表单,用户可以在其中输入图像文件,现在我想将图像文件发送到后端并保存在那里。 该文件以获取多部分形式发送,问题是 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 - 更多在这个线程

暂无
暂无

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

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