我正在尝试将媒体文件转换为base 64以在javascript中上传到服务。我正在离子2中构建一个应用程序,并使用离子原生的MediaCapture插件捕获图像。然后,将该图像转换为base64,如下所示:
MediaCapture.captureImage(options)
.then(
(data: any) =>{ //MediaFile[]
let imageLocalUrl = data[0].localURL;
console.log("imageLocalUrl:"+imageLocalUrl);
this.base64Lib.convertToBase64(imageLocalUrl).subscribe((iBase64Str)=>{
this.imageBase64 = JSON.stringify(iBase64Str).split("base64,")[1].replace(/(^")|("$)/g, '');
console.log("imageBase64:"+this.imageBase64);
})
// console.log(data)
},
(err: CaptureError) => console.error(err)
);
base 64转换:
convertToBase64(path){
return Observable.create((observer)=>{
let filePath = path;
console.log("path in b64lib:"+path);
(window as any).resolveLocalFileSystemURL(path,gotFile,failed);
function gotFile(fileEntry){
console.log("fileEntry:"+fileEntry);
// observer.next(fileEntry);
fileEntry.file(function(file){
var fileReader = new FileReader();
fileReader.onloadend = function(e){
var content = (e as any).target.result;
// console.log("event:"+e);
console.log("content:"+content);
observer.next(content);
// observer.complete();
}
fileReader.readAsDataURL(file);
})
}
任何人都可以帮我解决它的错误吗? 我使用localUrl,因为当我尝试通过fullpath使用文件时,它无法找到。我想只有持久存储才能使用fullpath。