【问题标题】:onDownloadStart parameters of webview not set? How to retrieve filename?webview的onDownloadStart参数没有设置?如何检索文件名?
【发布时间】:2019-02-14 06:44:43
【问题描述】:

我有 javascript 代码,我使用锚标记的下载属性来设置文件名。 以下是我的代码 sn-p:

const fileReader = new FileReader();
fileReader.onload = () => {
const link = <HTMLAnchorElement>windowService.document.createElement("a");
link.href = fileReader.result;
link.setAttribute("download", fileName);
link.target = "_blank";

windowService.document.body.appendChild(link);
link.click();
};
fileReader.readAsDataURL(testFile);
}

其中 testFile 是数据块。此代码适用于所有浏览器、iOS 应用程序,但不适用于 android 应用程序。 在 Android webView 上,调用了 onDownloadListener 函数,只设置了 url 参数,没有设置其他参数。

    webView.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
// url: set as base64 encoded content
// no other attribute is set. I understand content disposition can be used to get file name but that is not set either
}
}

我想在锚点的下载属性中获取分配给文件的文件名。如何检索相同的文件名?我正在使用 Android API 28 进行测试。

【问题讨论】:

    标签: javascript java android android-webview


    【解决方案1】:

    我用URLUtil 工作得很好,很容易

    webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
                Toast.makeText(MainActivity.this, filename, Toast.LENGTH_SHORT).show();
            }
        });
    

    【讨论】:

    • 这对我不起作用,因为 contentDisposition 和 mimeType 中没有数据
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多