【发布时间】:2017-06-09 23:07:33
【问题描述】:
尝试通过 Angular $http 上传文件。我正在设置 Content-Type 并按照大多数地方的建议用 FormData 对象包装它。但是,浏览器在 http 请求的请求负载中发送 [object data]。
服务调用:
let fd = new FormData();
fd.append('file', file);
return $q((resolve, reject) => {
$http.post(url, fd, {
headers: {
'Content-Type': undefined,
'X-Token': undefined
}
}).then(
success => resolve(success.data),
error => reject(error)
);
});
关于为什么浏览器不能编码这个文件的想法?请注意,到目前为止,我已经尝试过 docx、zip、mp4 文件格式。它们都产生相同的结果。
请求负载:
------WebKitFormBoundaryqfOlKmKqvArA9pz6
Content-Disposition: form-data; name="file"
[object File]
------WebKitFormBoundaryqfOlKmKqvArA9pz6--
使用的标记:
<input type="file" id="file-tutorial-{{$index}}-{{language.iso2}}" name="file-tutorial-{{$index}}-{{language.iso2}}"
onchange="angular.element(this).scope().ctrl.addFile(this);">
Ctrl.addFile:
addFile(element) {
service.uploadFile(element.files[0]);
this.$scope.$digest();
}
element.files[0] 或文件对象:
{lastModified:1496720186000
lastModifiedDate:Mon Jun 05 2017 23:36:26 GMT-0400 (EDT)
name:"Adil Sulaiman Resume.docx"
size:25456
type:"application/vnd.openxmlformats-
officedocument.wordprocessingml.document"
webkitRelativePath:""}
【问题讨论】:
标签: javascript angularjs http