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

时间:2018-12-06 08:11:38

标签: 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);
                    }
                }));
            });
        }

请帮助我。 谢谢

2 个答案:

答案 0 :(得分:1)

Android DownloadManager被指定为仅使用HTTP / HTTPS协议下载远程文件。 Blob不是远程数据,而是WebView中的某个数据。因此,解决方法是将Blob转换为Base64字符串并将其写入文件。

Here是另一个具有不错示例的SO线程。

答案 1 :(得分:1)

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

background = pygame.image.load(os.path.join(MYDIR, 'example.jpg'))
nubjuk = pygame.image.load(os.path.join(MYDIR, 'nubjuk.png'))