我想使用类型的post请求上传多个文件,multipart/form-data并且我需要知道服务器端的文件大小(内容长度).
为了构造POST请求,javascript我使用一个FormData对象并将File对象附加到它上面.这样可以正常工作,但除了Content-type标题之外,每个部分只添加一个Content-Disposition标题,但没有Content-length标题,尽管可以从各个File对象获得此信息.
有没有办法实现在发送请求时Content-length为FormData对象的每个部分设置标头?
下面是我使用的代码,包括我对问题的解决方法.它实际上用于angular-js发送请求,但我认为这与问题无关.
var form = new window.FormData();
form.append('additional-field-1', new Blob(['some plain text'], {type : 'text/plain'}));
for (var file in fileList) {
var fileObj = fileList[file];
var count = 1 + parseInt(file, null);
form.append('file-size-' + count, new Blob([fileObj.size], {type : 'text/plain'}));
form.append('file-' + count, fileObj);
}
$http.post(url, form, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).success(.....
Run Code Online (Sandbox Code Playgroud)