任何以javascript数据丢失为基础的文件64

时间:2017-01-23 16:38:39

标签: javascript base64 ionic2 cordova-plugins

我正在尝试将媒体文件转换为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);
    })
  }

当我尝试将控制台输出转换为图像时,我将其视为: enter image description here

任何人都可以帮我解决它的错误吗? 我使用localUrl,因为当我尝试通过fullpath使用文件时,它无法找到。我想只有持久存储才能使用fullpath。

base64转换参考:http://ourcodeworld.com/articles/read/80/how-to-convert-a-image-from-the-device-to-base64-with-javascript-in-cordova

0 个答案:

没有答案