2013-04-05 41 views
0

中似乎什麼也不做什麼我遇到了Phonegap FileTransfer API的問題。目前,我試圖運行下面的示例代碼:Phonegap FileTransfer在android

 saveFile: function (filepath,location, filetype) { 
      console.log("FUNCTION BEING CALLED"); 
      var self = this; 
      //filepath is path to the internets file 
      //location is where on the device the file will be stored 
      var fileTransfer = new FileTransfer(); 
      fileTransfer.download(filepath,self.rootPath + location + '.' + filetype, 
       function (entry) { 
        console.log('############# DOWNLOAD WORKS ##############'); 
        console.log("download complete: " + entry.fullPath); 
       }, 
       function (error) { 
        console.log('############# DOWNLOAD FAILS ##############'); 
        console.log("download error source " + error.source); 
        console.log("download error target " + error.target); 
        console.log("upload error code" + error.code); 
       } 
      ) 
     } 

目前,什麼都似乎發生。沒有錯誤被標記,並且回調似乎沒有被解僱。我所得到的是「FUNCTION BEING CALLED」的初始console.log。

我已經雙重檢查過,並且可以確認我包含cordova js文件(cordova-2.4.0.js)。

如果有問題,我已經在Android模擬器,HTC One X和三星Galaxy S3上嘗試了這一切,所有結果都一樣。

有誰知道可能是什麼原因造成的?

編輯:我也覺得我應該提到它也都運行在Angular JS應用程序中。

+0

嘗試console.log(self.rootPath + location +'。'+ filetype);並檢查你得到了什麼 – 2013-04-05 12:27:26

+0

這給了我'/ test_app/test_pdf.pdf'。我也嘗試過沒有根,這給了我'test_pdf.pdf'。這可能是問題的原因嗎?我需要指定一個特定的路徑嗎? – Sam 2013-04-05 12:45:15

+0

我也嘗試過使用'file:/// sdcard/theFile.pdf',但我仍然沒有任何東西 – Sam 2013-04-05 13:22:25

回答

0

這就是我如何做它在我的項目

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    } 
function gotFS(fileSystem) { 
    window.fileSystem = fileSystem; 
    //create directory test_app 
    fileSystem.root.getDirectory("test_app", { 
     create : true, 
     exclusive : false 
    }, dirWallpaper, fail); 

function fail(evt) { 

} 

function dirWallpaper(entry) { 
    //save the directory path 
    window.testDir = entry; 
} 
//now download file to that location 
saveFile: function (filepath,location, filetype) { 
     console.log("FUNCTION BEING CALLED"); 
     var self = this; 
     //filepath is path to the internets file 
     //location is where on the device the file will be stored 
     var fileTransfer = new FileTransfer(); 
     var path = window.testDir.fullPath + "/test_pdf."+ filetype; 
     fileTransfer.download(filepath,path, 
      function (entry) { 
       console.log('############# DOWNLOAD WORKS ##############'); 
       console.log("download complete: " + entry.fullPath); 
      }, 
      function (error) { 
       console.log('############# DOWNLOAD FAILS ##############'); 
       console.log("download error source " + error.source); 
       console.log("download error target " + error.target); 
       console.log("upload error code" + error.code); 
      } 
     ) 
    } 

嘗試像這樣,讓我知道。

+0

看來我的問題可能比我第一次想到的更基礎。事實證明'deviceready'事件永遠不會爲我開火,'window.requestFileSystem()'也沒有任何作用。經過一番挖掘,並將console.logs放到cordova.js中後,我發現javascript工作正常,但當它遇到'exec()'函數時,一切就停止了。所以這導致我相信電話本身不能正常工作? – Sam 2013-04-05 15:59:04

+0

然後我想這是項目創建的問題,嘗試下載最新的phonegap並使用他們在其網站上提供的說明爲android創建項目。 – 2013-04-05 19:46:55

+0

我解決了我的問題。如果你有興趣,當我設置這個項目時,我複製了iOS版本的代碼,這意味着我試圖運行app版本的cordova.js的iOS版本,而不是android版本。我害怕PEBKAC一個明確的例子。非常感謝您的幫助 – Sam 2013-04-05 23:24:27

相關問題