[英]How to POST blob using angularjs and receive blob in C# to Email
[英]How to make a POST request for a blob in AngularJS
我有以下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中做同樣的事情?
不是將blob附加到FormData ,而是直接發送blob更有效,因為FormData API的base64編碼有額外的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方法可以設置內容類型標頭。 否則,AngularJS框架將覆蓋內容類型application/json
。
有關更多信息,請參閱AngularJS $ http服務API參考 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.