Alfz
Alfz

Reputation: 97

Problems with download link in phonegap android

I'm trying to download a dynamically generated file on the server. Trying a simple application using phonegapbuild 1.4.1.

First I've tried a direct link to a server page wich returns the file via http with "content-type" and "content-disposition: attachment; filename=" headers. This download link works ok in a regular browser. But when clicking this link in a phonegap application doesn't seem to work (at least in android 2.3.3), when the link is clicked the call is made to the server, but then nothing happens.

Then I've discovered a phonegap api function named FileTranfer.download. I don't know how to specify the file path, how can I know the default download location (cross-platform)? I've tried the fileSystem.resolveFileSystemURI function, but nothing happens (no success or fail event), also i've tried the following sentence fileSystem.root.getDirectory( "download", { create: true } ); and it hangs there, the next line which is an alert is never executed.

Can anybody please help me, and point me to a reliable way to download an attachment (preferably via the direct link to the server)

Upvotes: 1

Views: 889

Answers (2)

Anuj Dubey
Anuj Dubey

Reputation: 347

If the problem is with only android then this may help you and I have tried it in Android, Hope this will work with IOS also.

enter code here
 function fun(){
var dfd = $.Deferred(function (dfd){
var remoteFile = "Your link";
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localFileName, {create: true, exclusive: false},
    function(fileEntry) {
var localPath = fileEntry.fullPath;
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
            localPath = localPath.substring(7);
        }

    var ft = new FileTransfer();
    ft.download(remoteFile, localPath, function(entry) {
    dfd.resolve('file downloaded');
     // Do what you want with successful file downloaded and then
    // call the method again to get the next file
    //downloadFile();
            }, fail);
             }, fail);
            }, fail);

           });
            return dfd.promise();

        }
                fun().then(function(msg){
            if(msg==="file downloaded")
            {
            alert("Download complete");
            }
            else
            {
            alert("Download error")
            }
            });
            function fail(){
            alert("error");
        }

Upvotes: 1

Simon MacDonald
Simon MacDonald

Reputation: 23273

It is looking for a full path. So something like "/sdcard/download.txt" would work.

Upvotes: 0

Related Questions