簡體   English   中英

如何使用Phonegap將文件下載到Android的下載文件夾?

[英]How do you download a file to Android's Downloads folder using Phonegap?

我已使用Phonegap的File API成功將文件下載到我的Android手機。 我想將文件下載到手機上的“下載”文件夾中。 例如,如果從電子郵件下載附件,附件將轉到“下載”文件夾。 這是我的JS代碼,它將文件下載到“file:// mnt / sdcard /”:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
  fileSystem.root.getFile('myfile.jpg', {
    create: true, 
    exclusive: false
  }, function(fileEntry) {
    var localPath = fileEntry.fullPath,
        fileTransfer = new FileTransfer();        
    fileTransfer.download(uri, localPath, function(entry) {
      console.log("download complete: " + entry.fullPath);
    }, function (error) {
    console.log('download error: ' + error.code);
    console.log("download error source " + error.source);
    console.log("download error target " + error.target);
  });
  }, downloadError);
}, downloadError);

必須有一種方法來訪問Downloads文件夾,因為我在其他應用程序中始終看到此功能。

您可以通過在getFile方法中指定文件將文件發送到下載文件夾...

getfile('download/myfile.jpg' ...)

這不會觸發DownloadManager,它會在下載文件時通知您。 我仍在嘗試找到通過phonegap訪問DownloadManager類的解決方案。 我在這里問過這個問題你如何使用Phonegap將文件下載到Android的下載文件夾?

我創建了一個插件,使用下載管理器下載文件,並顯示進度條

https://github.com/vasani-arpit/cordova-plugin-downloadmanager

//after device is ready
var fail = function (message) {    
   alert(message)
}
var success = function (data) {
   console.log("succes");
}
cordova.plugins.DownloadManager.download("Your URL to download", success, fail);

我希望它有所幫助。

我遇到了同樣的問題,但我解決了這個問題:

//if IOS cordova.file.documentsDirectory
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (fileEntry) {
  var filepath = fileEntry.toURL() + filename;
  var fileTransfer = new FileTransfer();
  console.log('FilePath ' + filepath);
  fileTransfer.download(uri, filepath,
    function (fileEntry) {
      console.log("download complete: " + fileEntry.toURL());
    },
    function (error) {
      console.log("ErrorDownload: " + JSON.stringify(error));
    },
    true,
    {}
  );
});

使用此插件: https//github.com/sgrebnov/cordova-plugin-background-download 我在我的Cordova應用程序中使用它,它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM