onDownloadStart parameters of webview not set? How to retrieve filename?

Viewed 525

I have javascript code where I use anchor tag's download attribute to set filename. Following is my code snippet:

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);
}

where testFile is blob of data.This code works fine with all the browsers, iOS app but not with android app. On Android webView, onDownloadListener function is called and just url parameter is set and no other parameters are set.

    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
}
}

I want to get the filename assigned to file on download attribute of anchor. How can I retrieve file name for the same? I am using Android API 28 for testing.

1 Answers

I used URLUtil work well and easily

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();
        }
    });
Related