chr*_*rjs 6 javascript ajax angularjs
我想将二进制文件(图像)上传到接受application/octet-stream. 不幸的是,似乎 angular 想要改变我的请求,这显然不起作用并导致
TypeError: key.charAt is not a function
我的请求是这样的:
var f = document.getElementById('file_picker').files[0],
r = new FileReader();
r.onloadend = function(e){
var fileData = e.target.result;
$http({
url: '.../upload/' + id,
method: 'PUT',
headers: {'Authorization': 'Bearer asdf',
'Content-Type': 'application/octet-stream'},
data: new Uint8Array(fileData),
transformRequest: []
})
})
r.readAsArrayBuffer(f);
Run Code Online (Sandbox Code Playgroud)
这是我通过 curl 的请求,它有效:
curl -i -X PUT -H "Authorization: Bearer asdf" -H "Content-Type: application/octet-stream" --data-binary @jpg.jpg https://.../upload/123
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢。
工作解决方案:
var file = document.getElementById('file_picker').files[0],
reader = new FileReader();
reader.onload = function() {
var f = new Uint8Array(reader.result);
$http({
url: '...upload/,
method: 'PUT',
headers: {'Authorization': 'Bearer asdf',
'Content-Type' : undefined},
data: f,
transformRequest: []
});
};
reader.readAsArrayBuffer(file);
Run Code Online (Sandbox Code Playgroud)
问题是,我们配置了一个数据映射拦截器,它将所有 post 和 put 请求转换为 json。哎哟。
| 归档时间: |
|
| 查看次数: |
2490 次 |
| 最近记录: |