1

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();
Benjamin E.
  • 5,042
  • 5
  • 38
  • 65
  • Could you provide some code how you solved this problem? Is that a bug in Cordova? We have the same problem. – christianmenkens Nov 19 '13 at 20:26
  • Don't know if FileTransfer works now, haven't tried in a while. See code example above. You need to know the mimeType to construct a correct blob, but that can be just text I guess. If you need a reference for the FileSystemAPI, look up Filer.js by Eric Bidelman. – Benjamin E. Nov 20 '13 at 03:12

4 Answers4

3

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!

christianmenkens
  • 790
  • 4
  • 22
  • Hi @christianmenkens my i know what did you did on the success callback return to display it on image src? i got success but it returns var/mobile ... – BizApps Mar 12 '14 at 10:08
  • a path with file://var/mobile/Applications/D702F059-A29F-4FF4-A165-D4A903DEDE7D/Documents/captured/2419747919.jpeg should be fine to work with on iOS. We are just using this path to put it into FileUpload. – christianmenkens Mar 20 '14 at 22:15
  • @christianmenkens are you sure there are just two slashes after file: ? I get three when requesting FS ... (and cocoa error 512.. works well on Android) – trainoasis Jul 08 '14 at 10:51
  • 1
    I use dirEntry.nativeURL+filename as the target for FileTransfer.download on iOS and Android and it works like a charm! – 695Multimedia Nov 14 '14 at 15:46
0

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

brickpop
  • 2,754
  • 1
  • 13
  • 12
0

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

Seraj Ahmad
  • 405
  • 6
  • 10
0

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

wmarbut
  • 4,595
  • 7
  • 42
  • 72