【问题标题】:Getting image null vuejs symfony获取图像 null vuejs symfony
【发布时间】:2020-05-08 16:13:21
【问题描述】:

我正在尝试将图像从 vuejs 上传到服务器,但在服务器端我无法抓取图像。我每次都收到null。这是vuejs代码:

    submitFile() {
        let formData = new FormData();
        formData.append('file', this.file);
        fetch('/upload-image', {
            method: 'post',
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            body: formData
        }).then(response => {
            return response.json()
        }).then(data => {
            console.log(data)
        })
    }

这就是 symfony:

/**
 * @Route("/upload-image", methods={"POST"})
 * @param Request $request
 * @return Response
 */
public function uploadImage(Request $request) {
    $data = $request->files->get('file');
    return new JsonResponse(['data' => $data]);
}

返回的数据是null

【问题讨论】:

  • 能否在 fetch 中添加 .catch(function(error) { console.log(error); }); 以及返回控制台日志的内容

标签: symfony vue.js


【解决方案1】:

有点不清楚this.file 来自哪里。您可以轻松获取文件输入。

const selectedFile = document.querySelector('input').files[0];
formData.append(selectedFile);

更多信息请查看MDN

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 1970-01-01
    • 2018-10-02
    • 2010-10-23
    • 1970-01-01
    • 2019-05-26
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多