File Ionic摄像头文件\u uri转换为blob以便s3上传
我正在开发一个功能,在从本机相机捕获图像后,我正在上传图像。但将文件uri转换为blob时存在问题。它不会继续读取ArrayBuffer。是否有其他将文件uri转换为blob的解决方案File Ionic摄像头文件\u uri转换为blob以便s3上传,file,ionic-framework,amazon-s3,camera,file-uri,File,Ionic Framework,Amazon S3,Camera,File Uri,我正在开发一个功能,在从本机相机捕获图像后,我正在上传图像。但将文件uri转换为blob时存在问题。它不会继续读取ArrayBuffer。是否有其他将文件uri转换为blob的解决方案 fileUriToBlob(imageData) { return new Promise((resolve, reject) => { let fileName = ""; this.file .resolveLocalFilesystemUrl(image
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));
});
}
资料来源:
编辑:
试过这个
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;
}
imageToBlob(b64Data:string,contentType:string,sliceSize:number=512){
contentType=contentType | |“”;
切片大小=切片大小| | 512;
让byteCharacters=atob(b64Data.replace(/^data:image\/(png | jpeg | jpg);base64,/,'');
let ByteArray=[];
for(让offset=0;offset
但我遇到了这个错误:DomeException:未能在“窗口”上执行“atob”:要解码的字符串编码不正确。您需要确保已安装cordova插件文件
请参阅爱奥尼亚文档-请随意在回购协议中打开一个问题,我会看一看-aaronksaundersi刚刚检查了代码,它确实有效,请发布有关真实问题的其他信息。readAsArrayBuffer似乎无法解决。你遇到过这种情况吗?