我有javascript代码,我使用 anchor 标记的下载属性来设置文件名。
以下是我的代码 fragment :
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 是数据 block 。此代码适用于所有浏览器、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
}
}
我想在 anchor 的下载属性上获取分配给文件的文件名。如何检索相同的文件名?我正在使用 Android API 28 进行测试。
最佳答案
我用了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();
}
});
关于javascript - webview的onDownloadStart参数没有设置?如何检索文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247505/