简体   繁体   English

如何使用Phonegap将文件下载到Android的下载文件夹?

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

I have successfully downloaded a file to my Android phone using Phonegap's File API. 我已使用Phonegap的File API成功将文件下载到我的Android手机。 I would like to download the file to the Downloads folder on my phone. 我想将文件下载到手机上的“下载”文件夹中。 For example, if you download an attachment from an email, the attachment goes to your Downloads folder. 例如,如果从电子邮件下载附件,附件将转到“下载”文件夹。 Here is my JS code, which downloads the file to "file://mnt/sdcard/": 这是我的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);

There has to be a way to access the Downloads folder, because I see this functionality all of the time in other apps. 必须有一种方法来访问Downloads文件夹,因为我在其他应用程序中始终看到此功能。

You can send the file to the download folder by specifying it in the getFile method... 您可以通过在getFile方法中指定文件将文件发送到下载文件夹...

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

This doesn't trigger the DownloadManager which notifies you when a file has been downloaded. 这不会触发DownloadManager,它会在下载文件时通知您。 I am still trying to find a solution for having access to the DownloadManager class through phonegap. 我仍在尝试找到通过phonegap访问DownloadManager类的解决方案。 I have asked this question here How do you download a file to Android's Downloads folder using Phonegap? 我在这里问过这个问题你如何使用Phonegap将文件下载到Android的下载文件夹?

I have created a plugin which downloads file using download manager and shows progress bar along the way 我创建了一个插件,使用下载管理器下载文件,并显示进度条

https://github.com/vasani-arpit/cordova-plugin-downloadmanager 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);

I hope it helps. 我希望它有所帮助。

I had the same problem, but I solved it like this: 我遇到了同样的问题,但我解决了这个问题:

//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,
    {}
  );
});

Use this plugin: https://github.com/sgrebnov/cordova-plugin-background-download . 使用此插件: https//github.com/sgrebnov/cordova-plugin-background-download I use it in my Cordova app and it works fine. 我在我的Cordova应用程序中使用它,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用phonegap下载文件到下载文件夹ios / android - Download file to downloads folder ios/android using phonegap 如何将文件下载到Android中的内部下载文件夹中 - How to download file into internal downloads folder in Android 如何将文件下载到我的下载文件夹android - How can I download a file to my downloads folder android 如何使用Android Google Drive下载.zip文件夹 - How do you download folder as .zip using android google drive 第三方应用程序可以在“下载”文件夹中找到生成的文本文件,但未显示在Android的默认“下载”应用程序中 - Generated text file can be found by third party app in Download folder but not shown in Android's default Downloads app 使用Android下载提供程序进行文件下载 - File download using Android downloads provider Android R – 使用范围存储 (MediaStore.Downloads) 在下载文件夹中创建一个文本文件 - Android R – Create a text file in Download folder using Scoped storage (MediaStore.Downloads) 使用Cordova FileTransfer将文件下载到设备的Downloads文件夹 - Download a file to Downloads folder of device using Cordova FileTransfer 如何将文件下载到本地下载文件夹? - How do i download files to the local downloads folder? 如何使用PhoneGap在Android SdCard中下载和保存文件(APK)? - How to download and save a file(APK) in android SdCard using PhoneGap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM