AngularJS如何发送多部分/混合 [英] AngularJS how to send multipart/mixed
问题描述
我工作的一个项目,我要上传JSON的一个小一点与文件,在AngularJS工作。
I am working on a project where I have to upload a small bit of JSON with a file, working in AngularJS.
我使用丹尼尔法里德的角文件上传书面code,它是工作,但,它总是发送的multipart / form-data的,边界=<凡是>
I've written code using Danial Farid's angular-file-upload, and it is working, except, it always sends "multipart/form-data, boundary=<whatever>"
不过,我的 的必须使用的multipart /混合。
However, I MUST use multipart/mixed.
这是我的电话:
$scope.upload = $upload.upload({
url: <my url>,
method: 'POST',
data: $scope.data,
file: file,
}).progress(function(evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function(data, status, headers, config) {
// file is uploaded successfully
console.log(data);
});
有没有办法来修改它的头被发送之前?
Is there a way to modify the headers just before it gets sent?
如果没有使用他的角文件上传,然后用另一种方法,希望无需推出自己的功能?
If not using his angular-file-upload, then by another method, hopefully without having to 'roll my own' function?
编辑1:
我只是不明白怎么会如此难以做出这种改变。当然,你可以添加
I just cannot understand how it can be so difficult to make this change. Of course you can add
headers: {'Content-Type': 'multipart/mixed'}
但是,这也绝对没有什么,因为没有边界。为什么不能有可以拉边界出路?类似
But this does absolutely NOTHING because there is no boundary. Why can't there be a way to pull the boundary out? Something like
headers: {'Content-Type': 'multipart/mixed, boundary=%b'}
我需要得到这方面的工作ASAP项目。
I need to get this working ASAP.
推荐答案
我不能再等下去了。我结束了我自己的滚动和它的工作。这里的code ...希望其他人可以受惠。
I couldn't wait any longer. I ended up rolling my own and it does work. Here's the code...hopefully others can benefit.
file_contents距离reader.readAsArrayBuffer输出($ scope.files [0]);
file_contents is the output from reader.readAsArrayBuffer($scope.files[0]);
您需要建立从preamble文本,文件数据和页脚斑点,因为否则的二进制数据附加到一个字符串会做的二进制文件数据转换,它将无法正常工作。
You need to build up a blob from the preamble text, the file data and the footer, because otherwise appending binary data to a string will do conversions on the binary file data and it will not work properly.
var epochTicks = 621355968000000000;
var ticksPerMillisecond = 10000;
var yourTicks = epochTicks + ((new Date).getTime() * ticksPerMillisecond);
var boundary='---------------------------'+yourTicks;
var header="--"+boundary+"\r\n";
var footer="\r\n--"+boundary+"--\r\n";
var contenttype="multipart/mixed; boundary="+boundary;
var contents=header+"Content-Disposition: form-data; name=\"json\"\r\n";
contents+="Content-Type: application/json\r\n";
contents+="Content-Length: "+JSON.stringify(data).length+"\r\n\r\n";
contents+=JSON.stringify(data)+"\r\n";
contents+=header+"Content-Disposition: form-data; name=\"image\"; filename=\""+$scope.files[0].name+"\"\r\n";
contents+="Content-Transfer-Encoding: binary\r\n";
contents+="Content-Type: "+$scope.files[0].type+"\r\n";
contents+="Content-Length: "+$scope.files[0].size+"\r\n\r\n";
blob=new Blob([contents,file_contents,footer]);
$http.post(restUrl+"user/avatar/uploadAvatar",blob,{'headers':{'Content-Type':contenttype}}).
success(function (data, status, headers, config) {
alert("success!");
}).
error(function (data, status, headers, config) {
alert("failed!");
});
这篇关于AngularJS如何发送多部分/混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!