ehretf
ehretf

Reputation: 163

Phonegap Filetransfert download: API create file upon error

I'm using fileTransfer.download method to retrieve PNG file from my server.

However when I try to download a file that doesn't exist, the API create a file (giving it the name passing in to the download method) on my phone's filesystem with a size of 0 bits.

Is this a normal behavior, I would expected no file creation upon error.

I can have some piece of code to manually delete the file but I'm wondering if this is really needed or if I'm misusing the API.

I'm using version 2.2.0

Thanks a lot.

Best Regards.

Florent.

Upvotes: 2

Views: 157

Answers (2)

Suhas Gosavi
Suhas Gosavi

Reputation: 2180

Hey take a look at my answer over here -

Phonegap - Save image from url into device photo gallery

and let me know. If there is still any problem. Include following in your config.xml

For Android -

<feature name="http://api.phonegap.com/1.0/file" />
<feature name="File">
       <param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
      <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
<feature name="Storage">
       <param value="org.apache.cordova.Storage" name="android-package"/>
</feature>

For IOS-

<plugin name="File" value="CDVFile" />
<plugin name="FileTransfer" value="CDVFileTransfer" />

Upvotes: 0

Anuj Dubey
Anuj Dubey

Reputation: 347

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);
            }// You need to write IOS instead of Android

    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

Related Questions