I am looking to send a post request with multiple files using the multipart/form-data
type. It's important for me to know the file size (content-length) of each file on the server side.
When constructing the POST
request in Javascript
, I utilize a FormData
object and add File objects to it for upload. However, only a Content-type
header and Content-Disposition
header are added to each part, without a Content-length
header, even though this information is available from the File objects themselves.
Is there a way to ensure that Content-length
headers are set for every part within the FormData
object when sending the request?
Below is the code snippet I am currently using, along with my workaround for the issue. While the code uses AngularJS
for sending the request, the main concern lies in setting the Content-length headers.
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(.....