2012-12-06 24 views
4

我正在製作一個應用程序,需要從我的網站下載圖像並將它們存儲在手機中,但是當我嘗試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 

我用科爾多瓦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) 

回答

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://」前綴,並且由於您沒有權限而無法保存爲「/」。

+0

工作很好,謝謝O/ –

+0

@Simon麥克唐納我試圖以同樣的方式,但我得到錯誤說的ReferenceError:未定義文件傳輸。可能是什麼問題請幫忙。 –

+0

沒有爲我工作。我正在測試'genymotion android emulator',我的路徑是'file:// storage/emulated/0/Download/nn.txt'。它可能是一個許可問題?另外,如何要求權限保存在'/'位置? –

2

我已經嘗試過,並在Android的這項工作,但我還沒有檢查在ios.And下載是成功的,因爲我在我的項目中使用它。

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

我試過你的代碼,它說'設備沒有定義'。刪除'設備'檢查,現在它顯示錯誤彈出。 –

+0

根據cordova 3.4中的['bug'](https://issues.apache.org/jira/browse/CB-6094),我們必須使用'.toURL()'而不是'.fullPath()'。 –

+1

如果未定義設備,則必須在app/res/xml/plugins.xml中添加權限,並添加以下行: app/AndroidManifest.xml添加這行 然後它不會顯示任何錯誤,如設備未定義。 –

相關問題