2

I'm using Cordova (5.4) to create apps for Android and Iphone. All goes fine, except I want to download images using the Cordova's plugin "FileTransfer" and I having some problems with the path.

If I use the FileTransfer like this:

       uri = encodeURI('http://example.com/myImage.png'),
            fileURL = '/sdcard/Download/' + 'myImage.png',
fileTransfer.download(
                uri,
                fileURL,
                function (entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);
                },
                false,
                {
                    headers: {
                        "authorization": 'Bearer ' + token
                    }
                }
            );

This works fine. But I would want a path that worked on Android and Iphone, (not a static one) and if it could be, that the user could see this images directly in their gallery.

Checking the plugin description I tried:

fileURL = 'cdvfile://localhost/persistent/myImg.png'

But this fails with the FileTrasferError:

"/data/data/com.aco.plus/files/files/myImg.png: open failed: ENOTDIR (Not a directory)"

Checking answers around I tried also:

uri = encodeURI('http://example.com/myImage.png');

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {

            fileTransfer.download(
                uri,
                fileSystem.root.toURL() + '/' + 'myImg.png',
                function (entry) {
                    console.log("download complete: " + entry.fullPath);
                },
                function (error) {
                    console.log(error);

                },
                false,
                {
                    headers: {
                        "authorization": 'Bearer ' + token
                    }
                }
            );
        });

And I got the same error.

I'm quite lost. Anyone knows what can I do? I'm quite sure that must be a better way to do it than static routes.

Luisma
  • 119
  • 1
  • 1
  • 10
  • I know its quite old question but are you able to fix this issue. as i am also facing trouble while saving the file to download folder. – Roxx Dec 15 '16 at 18:32
  • If download success, you should re-scan your device storage, because Cordova does not know if the file is downloaded. so i made a plugin , It is a plugin that updates the gallery after downloading. [cordova-plugin-gallery-refresh](https://www.npmjs.com/package/cordova-plugin-gallery-refresh) – Kwan Ung Park Jul 06 '17 at 09:32

2 Answers2

2

@Luisma,

Please find the sample code snippet to write pdf file in device using cordova file and file transfer plugin:

var fileTransfer = new FileTransfer();

if (sessionStorage.platform.toLowerCase() == "android") {
    window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
} else {
    // for iOS
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
}

function onError(e) {
    navigator.notification.alert("Error : Downloading Failed");
};

function onFileSystemSuccess(fileSystem) {
    var entry = "";
    if (sessionStorage.platform.toLowerCase() == "android") {
        entry = fileSystem;
    } else {
        entry = fileSystem.root;
    }
    entry.getDirectory("Cordova", {
        create: true,
        exclusive: false
    }, onGetDirectorySuccess, onGetDirectoryFail);
};

function onGetDirectorySuccess(dir) {
    cdr = dir;
    dir.getFile(filename, {
        create: true,
        exclusive: false
    }, gotFileEntry, errorHandler);
};

function gotFileEntry(fileEntry) {
    // URL in which the pdf is available
    var documentUrl = "http://localhost:8080/testapp/test.pdf";
    var uri = encodeURI(documentUrl);
    fileTransfer.download(uri, cdr.nativeURL + "test.pdf",
        function(entry) {
            // Logic to open file using file opener plugin
        },
        function(error) {
            navigator.notification.alert(ajaxErrorMsg);
        },
        false
    );
};
Gandhi
  • 11,875
  • 4
  • 39
  • 63
  • Hi Gandhi, if you should find duplicates please flag them a such. – bummi Mar 22 '16 at 11:57
  • @bummi, Could you please guide me on this as i m fairly new to this? Thanks in advance – Gandhi Mar 22 '16 at 12:17
  • This question and the question you [answered too](http://stackoverflow.com/q/36130527/1699210) share the same answers, which could indicate on should be closed as duplicate of the other one. I'm not familiar with cordova so I did not flag/vote. If you should think the are duplicates just flag one as a duplicate of the better one. Choose flag/dulplicate and and the link of the target. – bummi Mar 22 '16 at 12:23
  • @bummi, Thanks for your time and the detailed response. – Gandhi Mar 22 '16 at 12:57
  • Thank you so much @Gandhi for your help. Following your code as guide I managed to save an archive on the iPhone. – Luisma Apr 04 '16 at 14:42
  • @Luisma, glad to hear that. Happy coding. – Gandhi Apr 04 '16 at 15:35
  • what is the /testapp/ part of the path in this sample code ? – kris Jun 26 '17 at 08:46
  • @kris that creates a folder by name 'testapp' – Gandhi Jun 26 '17 at 10:16
1

For paths into the application, I like to use

https://github.com/apache/cordova-plugin-file

This maps the different paths on every operative system, so its transparent to you, even through different SO or versions, it just pick the correct one.

Happy coding!

Víctor
  • 3,029
  • 3
  • 28
  • 43
  • Thank you so much for your help, and sorry for the delay in the comment. I was already using the plugin you say,but I think I'm doing something wrong or I'm missing somethin: When I use, for example, cordova.file.applicationStorageDirectory + '/' + fName, it says it stores the file in " /data/data/com.aco.plus/myImg.jpg" but there's not a /data/data directory, and the image is not in the filesystem (and why /data/data? seems duplicated). And if I use something like "cdvfile://localhost/persistent/Download/"+ fName, I got a FileTrasferError like the one I showed before. What I'm doing wrong? – Luisma Feb 24 '16 at 09:38