我有以下ajax代码,它向服务器发出对blob的POST请求,并打印返回的数据。
function upload(blob){
var formData = new FormData();
formData.append('file', blob);
$.ajax({
url: "http://custom-url/record.php",
type: 'POST',
data: formData,
contentType: false,
processData: false,
success: function(data) {
console.log(data);
}
});
}我如何在AngularJS中做同样的事情?
发布于 2017-03-04 23:17:41
与将blob附加到FormData相比,直接发送blob更有效,因为base64 encoding的FormData API具有额外的33%的开销。
var config = { headers: { 'Content-Type': undefined } };
$http.post(url, blob, config)
.then(function (response) {
var data = response.data;
var status = response.status;
var statusText = response.statusText;
var headers = response.headers;
var config = response.config;
console.log("Success", status);
return response;
}).catch(function (errorResponse) {
console.log("Error", errorResponse.status);
throw errorResponse;
});同样重要的是,将内容类型标头设置为undefined,以便XHR send method可以设置内容类型标头。否则,AngularJS框架将使用内容类型application/json覆盖。
有关详细信息,请参阅AngularJS $http Service API Reference。
https://stackoverflow.com/questions/42597106
复制相似问题