Benjamin E.
Benjamin E.

Reputation: 5162

Phonegap 3.0 FileTransfer download not working

I'm downloading a multi-part mime encoded image to iOS like this:

var ft = new FileTransfer();
url = encodeURI(url);

ft.download(url, path, function(fileEntry) {}, function(err) {});

with

path = "file://localhost/var/mobile/Applications/D702F059-A29F-4FF4-A165-D4A903DEDE7D/Documents/captured/2419747919.jpeg"

and get the following error:

body: "Could not create path to save downloaded file: The operation couldn’t be completed. (Cocoa error 513.)"
code: 1 (file not found)
http status: 200

This hints to an invalid path, but I can't see anything wrong with it. I get the path like this:

path = fs.root.toURL();

Everything else works fine and files can be stored in exactly the same path by taking photos. Just not via a FileTransfer download.

Any ideas or a bug in Phonegap 3.0? Thanks!

UPDATE - Workaround

FileWriter works and now even saves blobs on iOS and Android. Example code:

var xhr = new XMLHttpRequest();

xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';

xhr.onload = function() {
    var blob = new Blob([xhr.response], {type: 'image/jpeg'});

    // save via FileWriter
};

xhr.send();

Upvotes: 1

Views: 4580

Answers (4)

wmarbut
wmarbut

Reputation: 4685

You'll want to use FileEntry.toURL() to get a path that looks like this:

cdvfile://localhost/persistent/path/to/file

See the documentation here: https://github.com/apache/cordova-plugin-file-transfer

Upvotes: 0

Seraj Ahmad
Seraj Ahmad

Reputation: 415

use nativeURL to get the prefix and append your file name to it and pass it to FileTransfer object it will work.

Upvotes: 0

brickpop
brickpop

Reputation: 2796

I had problems with that while working on the iOS Simulator, but once I tested it on the actual device, it worked.

Upvotes: 0

christianmenkens
christianmenkens

Reputation: 790

I found the problem in iOS:

The path: path = "file://localhost/var/mobile/Applications/D702F059-A29F-4FF4-A165-D4A903DEDE7D/Documents/captured/2419747919.jpeg"

does not work because it is an URL with "localhost" in it.

From FileEntry in Cordova one can get a string using "fullPath" and "toURL" ... on Android they work both to write a file. On iOS only the fullPath works ... the URL does not successfully write a file!

Upvotes: 3

Related Questions