Software Archive
Read-only legacy content
17061 토론

Fail to download file from internet using FileTransfer.download()

Odysseas_Z_
초급자
1,345 조회수

Hello,

I want to download a file (image, pdf, html) from the internet (given the link) to a specific directory that I have already created using

getDirectory()

function. I have tried every code I could find online for Cordova but still I get error code: 1 no matter what kind of link I give as input. Here's a snippet of the code:

 

function requestFStoDownload(){
    window.requestFileSystem(
        LocalFileSystem.PERSISTENT, 
        0, 
        function(fileSystem){
            fileSystem.root.getFile(filePath,
                                    {create: true, exclusive: true},
                                    function downloadFile(fileEntry) {              
                                        var fileTransfer = new window.FileTransfer();
                                        fileTransfer.download(
                                        uri,
                                        fileEntry.fullPath,
                                        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=="
                                            }
                                        });
                                    },
                                    function(err){
                                        console.log("Failed in getting/creating file: " + err);
                                    });
        },
        function(err){
            console.log("Failed in requesting a FS to download: " + err);
        });
}

The link is stored in the

uri

variable after passing it through

encodeURI()

function.

 

I have included all required plugins in the project. I'm running the latest version of XDK (I don't remember the id but I had an automatic update several days ago) and I'm testing/debugging on a Samsung Tab 3 8.0 - Android 4.4.2.

Are there any suggestions?

Thank you very much!

0 포인트
2 응답
PaulF_IntelCorp
1,345 조회수

Have you reviewed the docs on the main plugin pages? https://github.com/apache/cordova-plugin-file and https://github.com/apache/cordova-plugin-file-transfer. Note they reference an HTML5 Rocks article as the best guide: http://www.html5rocks.com/en/tutorials/file/filesystem/

Also, Make sure you're using one of the latest versions of the plugin, these file plugins seem to be getting changed frequently. You can see the version numbers by looking that the tags in the repos. You can specify a particular version by using the "pencil" icon in the Plugins section to change the version number.

0 포인트
Odysseas_Z_
초급자
1,345 조회수

Hi Paul,

Thank you for your response!

You were right, in the main plugin page of FileTransfer and their example of the download function they say:

"// !! Assumes variable fileURL contains a valid URL to a path on the device, // for example, cdvfile://localhost/persistent/path/to/downloads/"

Which is not the same as the example they have in the PhoneGap main page... http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileTransfer

So for others who may face this problem the correction of the code above is just writing:

fileTransfer.download(
                                            uri,
                                            fileEntry.toURL(), ...

 

Thank you very much!

0 포인트
응답