【发布时间】:2020-01-23 03:24:05
【问题描述】:
我正在开发一项功能,在从本机相机捕获图像后上传图像。但是将 file_uri 转换为 blob 存在问题。它不会继续 readAsArrayBuffer。还有其他将 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: Failed to execute 'atob' on 'Window': The string to be decoded is not correct encoding.
【问题讨论】:
-
随时在 repo 中打开一个问题,我会看看 - aaronsaunders
-
我刚刚检查了代码,它确实有效,请发布更多信息
-
找到了真正的问题。似乎 readAsArrayBuffer 没有解决。你遇到过这种情况吗?
标签: file ionic-framework amazon-s3 camera file-uri