android - 无法在 Android 上的 WebView 中下载 Blob 文件类型

标签 android angularjs webview nativescript

我想从 webview 下载文件,但每次都出现错误(只能下载 HTTP/HTTPS URI:blob:https://.。)

我在我的代码中使用这个:

ngOnInit() {
        let webview: WebView = this.webViewRef.nativeElement;

        if (isAndroid) {

            webview.on(WebView.loadStartedEvent, function () {
                webview.android.getSettings().setBuiltInZoomControls(false);
                webview.android.getSettings().setJavaScriptEnabled(true);
                webview.android.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webview.android.getSettings().setAllowFileAccess(true);
                webview.android.getSettings().setPluginsEnabled(true);
                webview.android.getSettings().setAllowContentAccess(true);
                webview.android.getSettings().setAllowFileAccess(true);
                webview.android.getSettings().setAllowFileAccessFromFileURLs(true);
                webview.android.getSettings().setAllowUniversalAccessFromFileURLs(true);
                webview.android.getSettings().setDomStorageEnabled(true);
                webview.android.getSettings().setDefaultTextEncodingName("utf-8");

                webview.android.setDownloadListener(new android.webkit.DownloadListener({
                    onDownloadStart(url, userAgent, contentDisposition, mimetype, contentLength) {
                        let request = new android.app.DownloadManager.Request(android.net.Uri.parse(url));
                        request.setMimeType(mimetype);
                        let cookies = android.webkit.CookieManager.getInstance().getCookie(url);
                        request.addRequestHeader("cookie", cookies);
                        request.addRequestHeader("User-Agent", userAgent);
                        request.setDescription("Downloading file...");
                        request.setTitle(android.webkit.URLUtil.guessFileName(url, contentDisposition, mimetype));
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                        request.setDestinationInExternalPublicDir(android.os.Environment.DIRECTORY_DOWNLOADS, android.webkit.URLUtil.guessFileName(url, contentDisposition, mimetype));
                        let dm = utils.ad.getApplicationContext().getSystemService(android.content.Context.DOWNLOAD_SERVICE);
                        dm.enqueue(request);
                    }
                }));
            });
        }

请帮我解决这个问题。 谢谢

最佳答案

Blob url 包含编码格式的 url,因此您必须先将 url 转换为 Base64,然后将数据存储在文件中。为此,您需要使用 JavascriptInterface

public class JavaScriptInterface {
private Context context;
    private NotificationManager nm;
    public JavaScriptInterface(Context context) {
        this.context = context;
    }

    @JavascriptInterface
    public void getBase64FromBlobData(String base64Data) throws IOException {
        convertBase64StringToPdfAndStoreIt(base64Data);
    }
    public static String getBase64StringFromBlobUrl(String blobUrl){
        if(blobUrl.startsWith("blob")){

            return "javascript: var xhr=new XMLHttpRequest();" +
                    "xhr.open('GET', '"+blobUrl+"', true);" +
                    "xhr.setRequestHeader('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8');" +
                    "xhr.responseType = 'blob';" +
                    "xhr.onload = function(e) {" +
                    "    if (this.status == 200) {" +
                    "        var blobPdf = this.response;" +
                    "        var reader = new FileReader();" +
                    "        reader.readAsDataURL(blobPdf);" +
                    "        reader.onloadend = function() {" +
                    "            base64data = reader.result;" +
                    "            Android.getBase64FromBlobData(base64data);" +
                    "        }" +
                    "    }" +
                    "};" +
                    "xhr.send();";


        }
        return "javascript: console.log('It is not a Blob URL');";
    }
    private void convertBase64StringToPdfAndStoreIt(String base64PDf) throws IOException {

        Log.e("base64PDf",base64PDf);
        String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
        Calendar calendar=Calendar.getInstance();
      ;
        final File dwldsPath = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS) + "/Report" +   calendar.getTimeInMillis() + "_.xlsx");
        byte[] pdfAsBytes = Base64.decode(base64PDf.replaceFirst("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,", ""), 0);
            Log.e("bytearrya",""+pdfAsBytes);
        FileOutputStream os;
        os = new FileOutputStream(dwldsPath, false);
        os.write(pdfAsBytes);
        os.flush();
        os.close();

        if(dwldsPath.exists()) {
            sendNotification();


            File dir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS) + "/Report" +   
calendar.getTimeInMillis() + "_.xlsx");
            Intent sendIntent = new Intent(Intent.ACTION_VIEW);
         String path=   dir.getAbsolutePath();

            Uri uri;
            if (Build.VERSION.SDK_INT < 24) {
                uri = Uri.fromFile(dir);
            } else {
                File file = new File(path);
                uri = FileProvider.getUriForFile((MainActivity)this.context, 
this.context.getApplicationContext().getPackageName() + ".provider", file);
//                    uri = Uri.parse("file://" + dir); 
            }
            sendIntent.setDataAndType(uri, "application/vnd.ms-excel");
            sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            try{
                context.startActivity(sendIntent);

            }catch (Exception e){
                Toast.makeText(context, "Np app found to view file", Toast.LENGTH_SHORT).show();
            }

        }

    }
}

关于android - 无法在 Android 上的 WebView 中下载 Blob 文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53647087/

相关文章:

javascript - AngularJS 中的新窗口

javascript - Angularjs 表达式不适用于 ng-bind 和 {{ }}

android - 从 Gradle 中的 Url 编译 Jar

java - 尝试在Unity3d中构建Android版本时遇到问题

angularjs - webpack 和 less 如何在 css 中包含图像 url

android - 检测 Webview 错误并显示消息

android - 授权错误

Android编程——点击监听器

android - 将音频文件流式传输到 soundpool load() Android

android - Android WebView onPageStarted/onPageFinished 方法详解