7

我已使用 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);

必须有一种方法可以访问“下载”文件夹,因为我一直在其他应用程序中看到此功能。

4

4 回答 4

3

您可以通过在 getFile 方法中指定文件来将文件发送到下载文件夹...

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

这不会触发下载管理器,它会在文件下载时通知您。我仍在尝试找到通过 phonegap 访问 DownloadManager 类的解决方案。我在这里问过这个问题如何使用 Phonegap 将文件下载到 Android 的下载文件夹?

于 2012-09-28T00:57:21.207 回答
2

我创建了一个插件,它使用下载管理器下载文件并一路显示进度条

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);

我希望它有所帮助。

于 2017-06-29T15:27:21.017 回答
1

我有同样的问题,但我是这样解决的:

//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,
    {}
  );
});
于 2016-06-30T04:52:17.880 回答
-1

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

于 2015-02-11T23:53:19.027 回答