问题描述
我正在处理一个hybird应用程序,它似乎很简单的方法从服务器使用HTML5锚下载属性下载pdf文件,这是正常工作正如预期使用下面的代码在桌面浏览器。
I am working on a hybird application and it seems very straightforward way to download a pdf file from server using HTML5 anchor download attribute and this is working exactly as expected using below code on desktop browsers.
<a href="/path/sample.pdf" download="Test.pdf">Download</a>
挑战:但是当我试图在我的Hybird应用程序中运行相同的代码,使用cordova 2.9.0 ,当在移动设备上调试应用程序时;
Challange: But when I am trying to run same code in my Hybird application, using cordova 2.9.0 , when debugging app on mobile; on clicking of Download nothing shows up and download does not start.
我缺少一些非常基本的东西吗?
Am I missing something very basic here?
请建议。
推荐答案
此代码适用于Android平台。
首先,在您的平台文件夹中打开文件 [appname] .java
:
appname\platforms\android\\ \\ src \com\ [appname] \app
接下来,设置webview的downloadListener,紧接 super.init();
This code is for Android platform.First, open the file [appname].java
in your platform folder:appname\platforms\android\src\com\[appname]\app
Next, set downloadListener for the webview, right after super.init();
这里是完整的代码:
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使用cordova-2.9.0不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!