Phonegap 3.0 FileTransfer download not working

2019-04-15 07:35发布

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();

4条回答
我想做一个坏孩纸
2楼-- · 2019-04-15 08:08

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

查看更多
不美不萌又怎样
3楼-- · 2019-04-15 08:22

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

查看更多
时光不老,我们不散
4楼-- · 2019-04-15 08:24

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

查看更多
孤傲高冷的网名
5楼-- · 2019-04-15 08:32

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!

查看更多
登录 后发表回答