Phonegap Filetransfer download

一曲冷凌霜 提交于 2019-12-13 07:53:06

问题


I'm new on stackoverflow. it's the first time that I have to use Phonegap and really I have a problem. I need to make a table and by clicking on each element starts to download a pdf file and create a new folder (if it does not exist). But I can not even download a file after compiling with phonegap. All the examples I saw, just download an image through onload.

<script type="text/javascript">
function downloadFile(){
 var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';
    var filePath = 'local/path/to/your/file';
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(url);
    fileTransfer.download(
        uri,
        filePath,
        function(entry) {
            console.log("download complete: " + entry.fullPath);
        },
        function(error) {
            console.log("download error source " + error.source);
            console.log("download error target " + error.target);
            console.log("upload error code" + error.code);
        },
        false,
        {
            headers: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
}
</script>

HTML

<td onclick="downloadFile()">Row 1</td>

回答1:


First of all your url is invalid included http multiple times

var url = 'http://http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';

change like this

var url = 'http://legalespymes.com.ar/legalespymes/abonos/aseguradoras.pdf';

If you are create multiple directory and store file inside that directory this may be create issue.(example [project/sample/local] not created same time. so file not download).And make sure file download plugins are available in config.xml and specify phonegap version.

Use this code to create multiple directory at time.

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

// create directory
function gotFS(fileSystem) {
    window.FS = fileSystem;
    var printDirPath = function(entry){
        console.log("Dir path - " + entry.fullPath);
    }

    createDirectory("local/path/to/your", printDirPath);
}
function fail() {
    console.log("failed to get filesystem");
}

function createDirectory(path, success){
    var dirs = path.split("/").reverse();
    var root = window.FS.root;
    var createDir = function(dir){
        console.log("create dir " + dir);
        root.getDirectory(dir, {
            create : true,
            exclusive : false
        }, successCB, failCB);
    };

    var successCB = function(entry){
        root = entry;
        if(dirs.length > 0){
            createDir(dirs.pop());
        }else{
            success(entry);
        }
    };

    var failCB = function(){
    };

    createDir(dirs.pop());
}

Now you write your file download code .



来源:https://stackoverflow.com/questions/24122799/phonegap-filetransfer-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!