简体   繁体   中英

Cordova FileTransfer Download - always returns error 3

I've ran into some problems while using cordova plugin File Transfer. Thats my code:

window.requestFileSystem(
    LocalFileSystem.PERSISTENT,
    0,
    function onFileSystemSuccess(fileSystem) {
        fileSystem.root.getFile(
            "dummy.html", {create: true, exclusive: false},
            function gotFileEntry(fileEntry) {
                var sPath = fileEntry.fullPath.replace("dummy.html", "");
                var fileTransfer = new FileTransfer();
                fileEntry.remove();

                fileTransfer.download( 'http://cordova.apache.org/images/cordova_bot.png', sPath + photo.original_name,
                    function (theFile) {
                        alert('success: ' + JSON.stringify(theFile));
                        console.log("download complete: " + theFile.toURI());
                        // showLink(theFile.toURI());
                    },
                    function (error) {
                        alert('error: ' + JSON.stringify(error));
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code: " + error.code);
                    },
                    true
                );
            })
    },
    function (error) {
        alert('error request: ' + JSON.stringify(error));
    }
);

The fileTransfer.download's error callback is being returned with error code 3, http 401. I already updated the File and FileTransfer plugins, my cordova version is 4.3.0. Also checked my config.xml for

<access origin="*" />

but it's there. I tried adding the header Connection: close, but no result. Tried setting the download's 4th param to its default (false) too - no luck.

Testing on Android tablet.

Anyone anything? Thanks!

Just found a "solution" to my problem. What I did was to downgrade the file-transfer plugin version from 0.5.0 to 0.4.8.

If someone ever face similar problem, do as below:

  1. Delete the existing file-transfer plugin by running 'cordova plugins list' and then 'cordova plugin remove name_from_list '
  2. Go tohttps://github.com/apache/cordova-plugin-file-transfer/releases and download the zip file of 0.4.8 release.
  3. Unzip the file in root of your cordova application
  4. Run 'cordova plugin add path_to_unzipped_folder '

That's it. Seems to be working well, at least the success callback is returned, didn't really test more of it.

I personally wouldn't create a file then remove it just to get a directory URL. You should be able to obtain just that by doing fileSystem.root.toURL() - fileSystem.root is a DirectoryEntry and so contains methods you'd expect to see on a DirectoryEntry.

Just a bit quicker.

Update

If you feel inclined to use the file delete method, you should be using toURL() on the FileEntry, not fullPath. I think toURL() returns a URL that can be used throughout HTML app.

But as I say, fileSystem.root.toURL() is preferable. Example code as follows:

Thus, your code becomes:

window.requestFileSystem(
    LocalFileSystem.PERSISTENT,
    0,
    function (fileSystem) {
        var url = 'http://cordova.apache.org/images/cordova_bot.png',
            dir = fileSystem.root.toURL() + photo.original_name,
            ft = new FileTransfer();

        ft.download(url, dir,
            function (fileEntry) {
                alert('Downloaded!');
            },
            function (error) {
                alert('Download error');
                console.log(dir);
            }
        );
    },
    function (error) {
        alert('Error getting file system');
    }
);

Try that and see what happens. Might sound stupid, but I presume photo.original_name is defined? And there's a '/' between the directory and the filename?

If your remote file needs VPN to access, make sure your device is connected to VPN.

In my case I was not connected to VPN on device.

Just change the download url to http://www .{{Domain name}}/abc.pdf

use full url with www

This worked for me

let pathToDownload = `${cordova.file.externalDataDirectory}`;
let fileTransfer   = new FileTransfer();    
let url            = encodeURI(`https://cordova.apache.org/images/cordova_bot.png`);

fileTransfer.download(url, pathToDownload + "image.png",
    (fileEntry) => {
        console.log('Downloaded!');
    },
    (error) => {
        console.log('Download error');           
    },                      
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM