2017-03-04 2 views
2

다음 blob을 서버에 대한 POST 요청을 만들고 반환 된 데이터를 인쇄하는 다음 ajax 코드가 있습니다.AngularJS에서 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에서 동일한 작업을 수행 할 수 있습니까?

답변

0

base64 encodingFormData API에 33 %의 추가 오버 헤드가 있으므로 직접 BLOB을 보내는 것이 더 효율적입니다.

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을 참조하십시오.

내가 georgeawg 위에 언급 할 수
0

, 그것은 잘 작동하지만 당신은 캐치 기능

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

에 오타가 있어요
관련 문제