0
我正在運行Ionic項目,我需要使用社交分享插件。 但是,要共享圖像,必須將其作爲base64。如何從www/img /加載圖像並使用離子/角度將其轉換爲base64?如何從www/img /加載圖像並將其轉換爲base64(Angular/Ionic)?
我正在運行Ionic項目,我需要使用社交分享插件。 但是,要共享圖像,必須將其作爲base64。如何從www/img /加載圖像並使用離子/角度將其轉換爲base64?如何從www/img /加載圖像並將其轉換爲base64(Angular/Ionic)?
要下載圖像,設置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