将 uri 转换为 ionic 5 中的 blob

时间:2021-02-03 12:58:08

标签: angular file ionic-framework file-upload

我正在使用文件选择器插件来选择 pdf。我得到以下格式的 uri。

content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F201215_makemyvideo_app_german_converted.pdf

   const buffer = await this.file.readAsArrayBuffer(uri, fileName);
   const fileBlob = new Blob([uri], { type: 'application/pdf' });

但我收到错误输入不是目录

如何将 uri 转换为 blob?

1 个答案:

答案 0 :(得分:1)

这会帮助你。

  // FILE STUFF
 makeFileIntoBlob(_imagePath) {
 // INSTALL PLUGIN - cordova plugin add cordova-plugin-file
 return new Promise((resolve, reject) => {
  let fileName = "";
  this.file
    .resolveLocalFilesystemUrl(_imagePath)
    .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);
      resolve({
        fileName,
        imgBlob
      });
    })
    .catch(e => reject(e));
 });
}

这是参考link

相关问题