PhoneGap resolveLocalFileSystemURI

22,745

PhoneGap will not let you read files outside of the [APP HASH]/Documents or [APP HASH]/tmp folders. Unless you can find a way to initialize your app with your data in one of these folders, you will have to get your data another way. I have found the below code to work. Basically it downloads the local file into your temp folder and gives you the file entry.

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs){
    fs.root.getFile("temp", {create: true, exclusive: false},
      function(entry){
        fileTransfer.download(
                Url, // the filesystem uri you mentioned
                entry.fullPath,
                function(entry) {
                    // do what you want with the entry here
                    console.log("download complete: " + entry.fullPath);
                },
                function(error) {
                    console.log("error source " + error.source);
                    console.log("error target " + error.target);
                    console.log("error code " + error.code);
                },
                false,
                null
        );
    }, function(){
        alert("file create error");
    });
}, null);
Share:
22,745
esdotzed
Author by

esdotzed

Working on several web-application projects.

Updated on July 06, 2020

Comments

  • esdotzed
    esdotzed almost 4 years

    I am trying to resolve this filesystem uri shown below:

    /var/mobile/Applications/9483756B-8D2A-42C5-8CF7-8D76AAA8FF2C/Shift.app/iqedata/5977e2e9239649d5a7e3b8a54719679f/06e2b8896e51472789fcc27575631f94.jpg
    

    Can any body tell me how to resolve this uri in PhoneGap and get the FileEntry by using the method showing below?

    window.resolveLocalFileSystemURI(Url, resOnSuccess, resOnError);
    

    I have tried to add "file://" or "//" before the uri but it doesn't work.

    Thanks.

  • esdotzed
    esdotzed about 11 years
    Thank you so much. This stuff has driven me crazy and your solution works perfectly. And another trick is the uri should start with file:///var/blablabla
  • Uncharted Space
    Uncharted Space about 11 years
    Glad it worked! I usually build my paths starting with location.href, which already has the file:///. That way I don't have to hard code the whole path.
  • esdotzed
    esdotzed about 11 years
    Great! Thanks for letting me know.
  • Shoaib Chikate
    Shoaib Chikate over 10 years
    window.resolveLocalFileSystemURI works fine in android for me. But does it would differ for ios. Please let me know.
  • Saurabh Android
    Saurabh Android about 10 years
    @UnchartedSpace its giving me error as "ReferenceError: fileTransfer is not defined" how to resolve this for run your code?
  • Uncharted Space
    Uncharted Space about 10 years
    @SaurabhAndroid You should post your code and problem as a new question. In the question, be sure to give your PhoneGap version along with some sample code and context, and which plugins you have installed.
  • Jason Washo
    Jason Washo over 7 years
    @SaurabhAndroid FileTransfer is a different plugin than File... you are probably missing that.