2016-12-09 107 views

回答

0

要下載圖像,設置responseType: "arraybuffer"

var config = { responseType: "arraybuffer"}; 

var bufferPromise = $http.get(imgUrl, config).then(function(response) { 
    return response.data; 
}).catch(functon (errorResponse) { 
    console.log("ERROR: " + errorResponse.status); 
    throw errorResponse; 
}); 

然後上傳它,將其轉換爲Blob並使用FormData API

var uploadConfig = { "Content-Type": undefined }; 

bufferPromise.then(function (buffer) { 
    var blob = new Blob([buffer], {type: "image/jpeg"}); 
    var fd = new FormData(); 
    fd.append("part0", blob, "filename.jpg"); 
    return $http.post(uploadUrl, fd, uploadConfig); 
}).then (function (response) { 
    console.log("Success"); 
}).catch (function (errorResponse) { 
    console.log("ERROR: " + errorResponse.status); 
}); 

瀏覽器會自動使用Base64編碼的blob並設置爲"Content-Type": "multipart/form-data"

隨着AngularJS框架,使用配置有"Content-Type": undefined防止框架使用"Content-Type": application/json

是非常重要的
相關問題