Cordova/Phonegap を使用して、基本的にサーバーからファイルをダウンロードし、ローカル SD カードに配置する小さなプログラムを作成しています。このアプリは今のところ Android でのみ動作します。サーバーから必要なファイルをダウンロードするコードバ ライブラリから fileTransfer を使用するこの機能があります。
this.downloadFile=function(){
var filePath = '';
var remoteFile = config.url + 'api_download';
var localFileName = this.constants.currentFileName;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, function(fileEntry) {
var localPath = fileEntry.fullPath;
var fullPath = fileEntry.fullPath
var metadata = fileEntry.getMetadata();
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
localPath = localPath.substring(7);
}
filePath = localPath;
var ft = new FileTransfer();
ft.download(remoteFile,
localPath, function(entry) {
// Now file is downloaded, we need the mimetype
var type = returnMimeType(fullPath);
if(type != ''){
CDV.WEBINTENT.startActivity({
action: CDV.WEBINTENT.ACTION_VIEW,
url: fullPath,
type: type},
function() {},
function() {console.log('Failed to open URL via Android Intent');}
);
console.log("Local Path is :"); console.log(fullPath);
}
}, fail);
}, fail);
}, fail);
}
これは問題なく、期待どおりに機能しますが、ユーザーはファイルがダウンロードされているかどうかわかりません。これを通知する方法がないので、この方法の代わりに Android のデフォルトのダウンロード マネージャーを使用できないかと考えていました。または、どうにかしてダウンロードの進行状況を表示できますか。問題も解決します。
ありがとう!