【问题标题】:File download not working in Android using cordova-2.9.0使用cordova-2.9.0在Android中无法下载文件
【发布时间】:2014-05-21 00:59:59
【问题描述】:

我正在开发一个混合应用程序,使用 HTML5 锚下载属性从服务器下载 pdf 文件似乎非常简单,并且在桌面浏览器上使用以下代码完全符合预期。

<a href="/path/sample.pdf" download="Test.pdf">Download</a>

挑战:但是当我尝试在我的 Hybird 应用程序中运行相同的代码时,使用 cordova 2.9.0 在移动设备上调试应用程序时;单击“下载”时没有显示任何内容,并且无法开始下载。

我在这里遗漏了一些非常基本的东西吗?

请提出建议。

【问题讨论】:

标签: html cordova download hybrid-mobile-app cordova-2.7.0


【解决方案1】:

此代码适用于 Android 平台。 首先,在您的平台文件夹中打开文件[appname].javaappname\platforms\android\src\com\[appname]\app 接下来,在super.init();之后为webview设置downloadListener

这里是完整的代码:

package com.[appname].app; import android.os.Bundle; import org.apache.cordova.*; public class [appname] extends CordovaActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); super.appView.setDownloadListener(new android.webkit.DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { android.util.Log.d("Logger","url : " + url + " userAgent: " + userAgent + " contentDisposition: " + contentDisposition + " mimeType: " + mimetype + " contentLength " + contentLength); android.net.Uri source = android.net.Uri.parse(url); // Make a new request android.app.DownloadManager.Request request = new android.app.DownloadManager.Request(source); // appears the same in Notification bar while downloading String filename = getFilename(contentDisposition); request.setDescription("This file will be saved in your downloads folder."); request.setTitle(filename); //add cookie on request header (for authenticated web app) String cookieContent = getCookieFromAppCookieManager(source.getHost()); request.addRequestHeader("Cookie", cookieContent); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { request.allowScanningByMediaScanner(); request.setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } // save the file in the "Downloads" folder of SDCARD request.setDestinationInExternalPublicDir(android.os.Environment.DIRECTORY_DOWNLOADS, filename); // get download service and enqueue file android.app.DownloadManager manager = (android.app.DownloadManager) getSystemService(android.content.Context.DOWNLOAD_SERVICE); manager.enqueue(request); } }); super.loadUrl(Config.getStartUrl()); //super.loadUrl("file:///android_asset/www/index.html"); }; public String getFilename(String contentDisposition){ String filename[] = contentDisposition.split("filename="); return filename[1].replace("filename=", "").replace("\"", "").trim(); }; public String getCookieFromAppCookieManager(String url){ android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance(); if (cookieManager == null) return null; String rawCookieHeader = null; // Extract Set-Cookie header value from Android app CookieManager for this URL rawCookieHeader = cookieManager.getCookie(url); if (rawCookieHeader == null) return null; return rawCookieHeader; }; }

【讨论】:

  • 这行不通。请参阅我上面的评论。 Android 网页视图不支持下载属性。
猜你喜欢
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2015-06-30
  • 1970-01-01
相关资源
最近更新 更多