How do you store a file locally using Apache Cordova 3.4.0

后端 未结 3 518
长情又很酷
长情又很酷 2021-02-01 10:46

I am having a problem storing a file locally on an iOS (or android) device using apache cordova\'s \"file\" plugin. The problem I believe is setting the path properly.

相关标签:
3条回答
  • 2021-02-01 11:25

    Check out these links :

    http://cordova.apache.org/docs/en/3.4.0/cordova_plugins_pluginapis.md.html#Plugin%20APIs https://github.com/apache/cordova-plugin-file/blob/dev/doc/index.md

    http://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html#File

    First and second links provide you information about the plugin File and how to install it.

    The third one show you how to use the File plugin.

    Everytime you need to do something with Cordova, check if a plugin is available to do it :)

    regards.

    0 讨论(0)
  • 2021-02-01 11:30

    Your file path contains a typo (or a grammar error):

    var fileURL = "cdvfile://localhost/persistant/file.png";

    You should write it as persistent.

    Correct code:

    var fileURL = "cdvfile://localhost/persistent/file.png";
    
    0 讨论(0)
  • 2021-02-01 11:37

    So far I have only tested this on Android, but I believe it should work as-is, or with little modification on IOS:

    var url = 'example.com/foo'
    
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
        fileSystem.root.getFile('foo_file', {create: true, exclusive: false},
            function(file_entry){
                var ft = new FileTransfer()
                ft.download(url, file_entry.toURL(), function(fe){
                    fe.file(function(f){
                        reader = new FileReader()
                        reader.onloadend = function(ev){
                            console.log('READ!', ev.target.result)
                        }
                        reader.readAsText(f)
                    })
                })
            }
        )
    })
    

    Note that I also needed the contents of the file, so the bit at the end may be omitted if you don't need the contents at the time of downloading.

    Also note that there is a far simpler method using window.saveAs but it's only available in Android 4.4.

    0 讨论(0)
提交回复
热议问题