How do you store a file locally using Apache Cordova 3.4.0

后端 未结 3 525
长情又很酷
长情又很酷 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: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.

提交回复
热议问题