2017-04-23 73 views
1

我讀過很多帖子,但沒有得到最終答案。 從這個link的代碼開始,我將我的文件下載到應用程序中。無論如何,我想看到它的「下載」文件夾。 我使用Android,但顯然我想要一個適用於iOS的解決方案。Cordova - 在下載文件夾中下載文件

回答

3

編輯

如果你已經知道該文件的路徑,你可以只移動它:

var storageLocation = ""; 
 
console.log(device.platform); 
 
switch (device.platform) { 
 

 
    case "Android": 
 
     storageLocation = 'file:///storage/emulated/0/'; 
 
     break; 
 
    case "iOS": 
 
     storageLocation = cordova.file.documentsDirectory; 
 
     break; 
 

 
} 
 

 

 
var fileUri = "file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp‌​/test.pdf" 
 

 
function moveFile(fileUri) { 
 
    window.resolveLocalFileSystemURL(
 
      fileUri, 
 
      function(fileEntry){ 
 

 
       var parentEntry = storageLocation + "Download"; 
 
       
 
       // move the file to a new directory and rename it 
 
       fileEntry.moveTo(parentEntry, "newFile.pdf", success, fail); 
 
         
 
      }, 
 
      errorCallback); 
 
}

原始

這裏是一個樣本我使用的一段代碼完成這個。它在Android上效果最好,由於應用程序沙箱化,iOS有點不同,因此您需要自行處理檢索文件。我也用科爾多瓦設備插件,以確定應用程序正在運行什麼設備,那麼我可以改變存儲路徑,以滿足:

var storageLocation = ""; 
 
console.log(device.platform); 
 
switch (device.platform) { 
 

 
    case "Android": 
 
     storageLocation = 'file:///storage/emulated/0/'; 
 
     break; 
 
    case "iOS": 
 
     storageLocation = cordova.file.documentsDirectory; 
 
     break; 
 

 
} 
 

 
window.resolveLocalFileSystemURL(storageLocation, 
 
    function (fileSystem) { 
 

 
     fileSystem.getDirectory('Download', { 
 
       create: true, 
 
       exclusive: false 
 
      }, 
 
      function (directory) { 
 

 
       //You need to put the name you would like to use for the file here. 
 
       directory.getFile("YOUR_FILE_NAME", { 
 
         create: true, 
 
         exclusive: false 
 
        }, 
 
        function (fileEntry) { 
 

 

 
         fileEntry.createWriter(function (writer) { 
 
          writer.onwriteend = function() { 
 
           console.log("File written to downloads") 
 
          }; 
 

 
          writer.seek(0); 
 
          writer.write(YOUR_FILE_HERE); //You need to put the file, blob or base64 representation here. 
 

 
         }, errorCallback); 
 
        }, errorCallback); 
 
      }, errorCallback); 
 
    }, errorCallback); 
 

 
var errorCallback = function(e) { 
 
    
 
    console.log("Error: " + e) 
 
    
 
}

然後來檢索目錄,你可以將文件列表用途:

window.resolveLocalFileSystemURL(storageLocation, 
 
    function (fileSystem) { 
 
    
 
     fileSystem.getDirectory('Download', { 
 
       create: true, 
 
       exclusive: false 
 
      }, 
 
      function (directory) { 
 

 
       var reader = directory.createReader(); 
 
       reader.readEntries(function (files) { 
 

 
        if (files.length == 0) { 
 

 
         console.log("No files found in downloads folder.") 
 

 
        } else { 
 

 
         $.each(files, function (i, v) { 
 

 
          console.log("File Name: " + files[i].name;) 
 

 
         }); 
 

 
        } 
 

 
       }, getFilesFail); 
 
      }, getFilesFail); 
 
    }, getFilesFail); 
 

 
var getFilesFail = function(e) { 
 
    
 
    console.log("Error: " + e); 
 
    
 
}

安裝該裝置的插件使用這個命令:

cordova plugin add cordova-plugin-device 

文檔這裏:

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/

+0

爲線writer.write(YOUR_FILE_HERE);我想我不能使用文件路徑作爲YOUR_FILE_HERE。實際上,我有:file:///data/user/0/com.arsdigitalia.myapp/files/files/MyApp/test.pdf,但400kB的原始文件沒有被很好地複製(我在文件中找到了80B的文件下載文件夾)。建議? – Zappescu

+0

好的,我用官方插件文檔(https://github.com/apache/cordova-plugin-file-transfer#download-a-binary-file-to-the-application-cache-)中解釋的readBinaryFile和我拿到了這個文件。最後的答案是:我使用文件管理器訪問文件,然後看到下載文件夾,無論如何,在Android 7.1(桌面上的圖標)的「官方」下載文件夾中,我看不到它。爲什麼? – Zappescu

+0

我編輯我的文章,包括一個功能,應該將文件從一個目錄移動到另一個。我認爲你在官方下載應用程序中看不到它的原因是因爲它沒有被Android下載管理器下載到該文件夾​​。 –