Surya Teja
Surya Teja

Reputation: 1526

Convert uri to blob in ionic 5

I am using the file chooser plugin to pick pdf. I'm getting uri in the following format.

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' });

But I'm getting the error input is not a directory

How to convert uri to blob?

Upvotes: 2

Views: 2358

Answers (1)

Hassan Ali
Hassan Ali

Reputation: 1029

This will help you.

  // 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));
 });
}

This is reference link

Upvotes: 2

Related Questions