2010-09-02 76 views
9

我已經設法成功保存了一個網頁(x/html),但我還想保存其中包含的圖像和mp4視頻,以便進一步在離線模式下可視化。下載圖片並在iPhone上本地保存Phonegap應用程序

我有權訪問iOS文件系統,因此我通過AJAX請求獲取代碼並稍後將其保存到文件來保存html。

我真的不知道如何去做視頻圖片。我有一臺服務器,可以從我的應用程序發送查詢,因此它僅顯示我需要下載的內容,並在必要時使用最佳標題。我只是不知道如何從客戶端「下載」它(Javascript)。

在此先感謝您的幫助。

+0

對此有何更新?你最終決定了什麼? – Crashalot 2011-10-17 10:52:39

回答

0

你只能在本地完成恐怕。 我通過使用NSURLConnection編寫的FileDownload PhoneGap插件來完成它。我傳入網址通過Javascript下載到插件,以及一個目標位置(它甚至給我下載進度)。

+0

嗨,你有可能分享你的代碼嗎?如果我能夠實現我期待的目標,我願意捐贈。 – StJimmy 2010-09-06 07:53:04

2

可以使用FileTransfer對象到遠程圖像下載到本地文件。

這是最新的官方樣片片段:

// !! Assumes filePath is a valid path on the device 

    var fileTransfer = new FileTransfer(); 
    var uri = encodeURI("http://some.server.com/download.php"); 

    fileTransfer.download(
     uri, 
     filePath, 
     function(entry) { 
      console.log("download complete: " + entry.fullPath); 
     }, 
     function(error) { 
      console.log("download error source " + error.source); 
      console.log("download error target " + error.target); 
      console.log("upload error code" + error.code); 
     }, 
     false, 
     { 
      headers: { 
       "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" 
      } 
     } 
    ); 
+0

如果我想將圖像保存在應用程序中,可以請告訴我什麼是'filepath'?像www/something/file.jpg。 – AtanuCSE 2014-06-11 06:44:59

+0

你是什麼意思內的應用程序?您需要將其保存在SDCard或內部存儲器目錄中。該網頁是在應用程序資源中編譯的,你不能在那裏寫。 – 2014-06-11 09:38:50

+0

啊!好的,我知道了。 – AtanuCSE 2014-06-11 09:40:39

0

我用我的iOS應用程序的項目這個片段:

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; 
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, fail); 

var target_directory=""; 

function fail() { 
    //alert("failed to get filesystem"); 
} 

function downloadImage(url, filename){ 
    alert("download just started."); 
    try{ 
     var ft = new FileTransfer(); 
     ft.download(
      url, 
      target_directory + filename, 
      function(entry) { 
       //alert("download complete!:" + entry.nativeURL); //path of the downloaded file 
      }, 
      function(error) { 
       //alert("download error" + error.code); 
       //alert("download error" + JSON.stringify(error)); 
      } 
     ); 
    } 
    catch (e){ 
     //alert(JSON.stringify(e)); 
    } 

} 

function success(fileSystem) { 
    target_directory = fileSystem.root.nativeURL; //root path 
    downloadImage(encodeURI("http://upload.wikimedia.org/wikipedia/commons/2/22/Turkish_Van_Cat.jpg"), "cat.jpg"); // I just used a sample url and filename 
} 
相關問題