Anonymous
Anonymous

Reputation: 61

Cordova File Plugin giving Security error on using readAsDataUrl

I am using FileChooser to choose a file from my internal storage of my phone and File plugin to convert it into base64 but this.file.readAsDataUrl is giving error "FileError{code:2, Message:SECURITY_ERR}"

I have all required permissions still the problem persists below is my code

let base=this
  if(this.platform.is('android')){
    this.fileChooser.open()
    .then(uri => {

      var array = uri.split("/");

      var path=uri.substring(0, uri.lastIndexOf("/") + 1);

      console.log("path"+path)
      base.file.readAsDataURL(path, uri).then(dataText=>{
        console.log(dataText)
        var data = dataText;

      },(error)=>{
      console.log(error)
      })
    //console.log(uri)

    })
    .catch(e => console.log(e));
  }

I am using Ionic 3, Angular4, Typescript, Firebase

Upvotes: 4

Views: 2209

Answers (1)

Gary Klasen
Gary Klasen

Reputation: 1070

I got the same error and was wondering that those errors always happen when file URIs start with "content://". With "file:///" everything was working out.

So, i used "File Path" Plugin by Ionic (https://ionicframework.com/docs/native/file-path/) to insert the link and transfer it into a local format, which always started with "file:///".

In your case, you need to call this.filePath.resolveNativePath(path) directly after the got the URI of the file opener to "translate" it.

Upvotes: 6

Related Questions