Ionic 2 download file in assets folder

I try to download file in application directory of project but it gives error : URI not supported by CordovaResourceApi

Elaborate about this error e.i what is your code like?

code is :
var url='http://retis.sssup.it/~lipari/courses/infbase2012/03.c_intro-handout.pdf
fileTransfer.download(url, cordova.file.applicationDirectory + “ebooks/” + fileName).then((entry) => {
console.log('download complete: ’ + entry.toURL());
this.url = entry.toURL();
Toast.show(‘download complete’, ‘1500’, ‘bottom’).subscribe(
toast => {
console.log(toast);
});

    }, (error) => {
          console.log('download fail  : ' + JSON.stringify(error));
      Toast.show('fail to download ', '1500', 'bottom').subscribe(
        toast => {
          console.log(toast);
        });
      // handle error
    });

and error is {“code”:1,“source”:“http://retis.sssup.it/~lipari/courses/infbase2012/03.c_intro-handout.pdf",“target”:“file:///android_asset/ebooks/http://retis.sssup.it/~lipari/courses/infbase2012/03.c_intro-handout.pdf”,“http_status”:200,“body”:null,“exception”:"URI not supported by CordovaResourceApi: file:///android_asset/ebooks/http://retis.sssup.it/~lipari/courses/infbase2012/03.c_intro-handout.pdf”}

Try this:

Use ionic file and file transfer native plugins and import in your component file

import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';

And now you use this Cordova plugins like this,

var URL = encodeURI('http://retis.sssup.it/~lipari/courses/infbase2012/03.c_intro-handout.pdf’);

var targetPath = this.file.documentsDirectory + 'ebooks/' ;

const fileTransfer: FileTransferObject = this.transfer.create();

fileTransfer.download(URL,targetPath+fileName,true)
          .then((entry) => {
               console.log('download complete: ’ + entry.toURL());
          }, (error) => {
               console.log('download fail: ' + error);
});

I also use file transfer native plugins it works when using documentsDirectory but give error for applicationDirectory and I want to download it in applicationDirectory (asstes folder) of project

Hi, @demokumar

because applicationDirectory is read-only,
Screenshot%20from%202018-02-19%2016-27-11

yes I know but for display pdf using pdf js it required path of file from assets folder :frowning: so how I do this ?

have you found a solution? I have the same problem

The assets folder is virtual. It doesn’t exist, so you cannot write anything to it at runtime. The only way to get anything in there is to bundle it into your application at build time.

Thank you for your answer
I am using a plugin (PDFTron) which only opens files from the assets folder
in my application I would like to download the files before opening them
i even tried to open the file which is on the server using its url, but i have the error CORS policy: No ‘Access-Control-Allow-Origin’

That needs to be resolved on the server side.