将图像路径转换为Blob

时间:2018-11-21 09:50:06

标签: android ionic-framework

我正在尝试在ionic 4 android中将二进制图像作为blob上传。我正在获取文件路径,该文件路径使用此link中所示的方法转换为blob。为了检查Blob是否正确,我将其转换为base64并尝试在here中生成图像。它没有生成。可能是什么问题?

makeFile(_imagePath, addfile) {
    if (addfile) {
      _imagePath = "file://" + _imagePath;
    }
    return new Promise((resolve, reject) => {
      console.log('_imagePath: ' + _imagePath);
      this.file
        .resolveLocalFilesystemUrl(_imagePath)
        .then((fileEntry: any) => {
          fileEntry.file(function (file) {
            console.log('makeFile file: ' + file);
            const myfiles: File = file;
            var reader = new FileReader();
            reader.readAsArrayBuffer(file);
            reader.onloadend = (evt: any) => {
              var imgBlob: any = new Blob([evt.target.result], { type: 'image/jpeg' });
              resolve([myfiles, imgBlob]);
            };

            reader.onerror = (e) => {
              console.log('Failed file read: ' + e.toString());
              reject(e);
            };
            //resolve(myfiles);
          })

        })
        .catch(e => reject(e));
    });
  }
this.camera.getPicture(options).then((output) => {
      console.log("output: " + output);
      this.makeFile(output, false).then((myArr) => {
        this.androidFile = myArr[0];
        const filesize = this.androidFile.size;
        const blob = myArr[1];
        const filetype = this.androidFile.type;
        const filename = this.androidFile.name;

        console.log('androidFile: ' + JSON.stringify(this.androidFile));
        console.log('blob: ' + JSON.stringify(blob));
        var reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onloadend = function () {
          const base64data = reader.result;
          console.log("base64data: "+base64data);
        }
        
      });

0 个答案:

没有答案