从url下载文件到cordova应用程序错误

时间:2012-12-06 08:03:22

标签: android cordova download

我正在制作一款需要从我的网站下载图片并将其存储在手机中的应用,但是当我尝试使用phonegap时,会向我显示可能发生的所有错误。我该怎么做才能纠正这个= /?

var fileTransfer = new FileTransfer();
fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    "/",
    function(entry) {
        alert("download complete: " + entry.fullPath);
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("upload error code" + error.code);
    });

显示的错误是:

Download error source " the url used"
download error target: " the target used  "
upload error code 1

我正在使用cordova 2.2.0

这是logcat错误日志:

 12-06 09:07:26.413: E/FileTransfer(2186): {"target":"\/","source":"http:\/\/developer.android.com\/assets\/images\/home\/ics-android.png","code":1}
12-06 09:07:26.413: E/FileTransfer(2186): java.io.FileNotFoundException
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer.getFileFromPath(FileTransfer.java:794)
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer.access$700(FileTransfer.java:62)
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer$4.run(FileTransfer.java:631)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:8)

你是如此接近,这只是你的目标文件是错误的。尝试:

var fileTransfer = new FileTransfer();
fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    "file://sdcard/ics-android.png",
    function(entry) {
        alert("download complete: " + entry.fullPath);
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("upload error code" + error.code);
    });

您需要“file://”前缀,因为您没有权限,所以无法保存为“/”。

答案 1 :(得分:2)

我已经尝试过,这项工作在Android中,但我还没有在ios中检查。下载是成功的,因为我在我的项目中使用它。

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");
        }