[英]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.