【发布时间】:2019-04-17 19:38:37
【问题描述】:
我想使用 formData 和 axios 将文件上传到 Symfony 3 后端,我的问题是文件未附加但其他文本输入成功发送到后端
let company = document.getElementById('company_photo');
let companyLogo = company.files[0];
let form =document.getElementById('my-form-id');
let data = new FormData(form);
data.append('profile', companyLogo, companyLogo.name)
const config = {
headers: {
'content-type': 'multipart/form-data'
}
}
axios.post(url, data, config)..................
问题在于,当我尝试从 Symfony 3 返回文件时
return new JsonResponse(['files' => $request->files->all()])
它只是返回空对象
profile:{}
所有其他输入类型的文本都成功发送到服务器
查看网络选项卡,有效负载看起来像
clients_per_month: 4
message: rurutrurturrurtuturturt
profile: (binary)
配置文件变为二进制
表格是这样的
<form id='some-id' method="POST" enctype="multipart/form-data">
<input type="text" name="blah" />
<input type="file" name="filenameexample" id="exampleid">
........................
查看控制台进行调试,
for(var key of data.entries()) {
console.log(key[0] + ','+key[1]);
}
导致
profile, [object File]
有解决这个问题的想法吗?
更新,文件长这样
console.log(company.files[0])
导致
File {name: "Capture.PNG", lastModified: 1546921233553,
lastModifiedDate: Tue Jan 08 2019 12:20:33 GMT+0800 (Pluto
Standard Time), webkitRelativePath: "", size: 735825, …}
lastModified: 1546921233553
lastModifiedDate: Tue Jan 08 2019 12:20:33 GMT+0800 (Venus
Standard Time) {}
name: "Capture.PNG"
size: 735825
type: "image/png"
webkitRelativePath: ""
__proto__: File
【问题讨论】:
标签: javascript symfony axios form-data