簡體   English   中英

使用cordova-2.9.0在Android下無法使用文件下載

[英]File download not working in Android using cordova-2.9.0

我正在開發一個hybird應用程序,使用HTML5錨點下載屬性從服務器下載pdf文件似乎非常簡單,這與桌面瀏覽器上使用下面的代碼完全一致。

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

Challange:但是當我試圖在我的Hybird應用程序中運行相同的代碼時,使用cordova 2.9.0,在移動設備上調試app時; 點擊下載沒有顯示和下載沒有開始。

我錯過了一些非常基本的東西嗎?

請建議。

此代碼適用於Android平台。 首先,在你的平台文件夾中打開文件[appname].javaappname\\platforms\\android\\src\\com\\[appname]\\app接下來,在super.init();之后立即為webview設置downloadListener 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;
    };

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM