2017-03-04 6 views
2

次のajaxコードで、サーバーへのblobのPOSTリクエストを行い、返されたデータを出力します。AngularJSでブロブの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で同じことをするにはどうすればよいですか?

答えて

0

FormData APIbase64 encodingが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"); 
    return response; 
}).catch(function (errorResponse) 
    console.log("Error"); 
    throw errorResponse; 
}); 

XHR send methodは、コンテンツタイプヘッダを設定することができるように、それは同様に重要undefinedにコンテンツタイプヘッダを設定します。それ以外の場合、AngularJSフレームワークはコンテンツタイプapplication/jsonで上書きされます。

詳細については、AngularJS $http Service API Referenceを参照してください。

0

私はgeorgeawgの上にコメントすることはできません、それは素晴らしい作品が、あなたがキャッチ機能

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"); 
 
    return response; 
 
}).catch(function(errorResponse) { 
 
    console.log("Error"); 
 
}); \t \t

でタイプミスを得ました
関連する問題