离子相机file_uri转换为 blob 以进行 s3 上传



我正在开发一项功能,在从本机相机捕获图像后对其进行更新。但是将file_uri转换为 blob 存在问题。它不会继续读取AsArrayBuffer。还有其他将file_uri转换为 blob 的解决方案吗?

fileUriToBlob(imageData) {
return new Promise((resolve, reject) => {
let fileName = "";
this.file
.resolveLocalFilesystemUrl(imageData)
.then(fileEntry => {
let { name, nativeURL } = fileEntry;
// get the path..
let path = nativeURL.substring(0, nativeURL.lastIndexOf("/"));
console.log("path", path);
console.log("fileName", name);
fileName = name;
// we are provided the name, so now read the file into a buffer
return this.file.readAsArrayBuffer(path, name);
})
.then(buffer => {
// get the buffer and make a blob to be saved
let imgBlob = new Blob([buffer], {
type: "image/jpeg"
});
console.log(imgBlob.type, imgBlob.size);
// pass back blob and the name of the file for saving
// into fire base
resolve({
fileName,
imgBlob
});
})
.catch(e => reject(e));
});
}

来源: https://github.com/aaronksaunders/ionic4-firebase-storage

编辑:

试过这个

imageToBlob(b64Data: string, contentType: string, sliceSize: number = 512) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
let byteCharacters = atob(b64Data.replace(/^data:image/(png|jpeg|jpg);base64,/, ''));
let byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
let slice = byteCharacters.slice(offset, offset + sliceSize);
let byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
let byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
this.blobImage = new Blob(byteArrays, { type: contentType });
return this.blobImage;
}

但是我遇到了这个错误:DOMException:无法在"窗口"上执行"atob":要解码的字符串未正确编码。

您需要确保已安装 cordova-plugin-file

请参阅 Ionic 文档 - https://ionicframework.com/docs/native/file

相关内容

  • 没有找到相关文章

最新更新