0

I am new to android development. I am using webview and there are pdf files in my website for download. But the user need to login to access the downloadable content. I used below code and downloading and file naming is perfect.But the file type is become html in every download and the html show only a webpage with error 404. Help me to fix this.

 webView.setDownloadListener(new DownloadListener()
   {

  @Override  


   public void onDownloadStart(String url, String userAgent,
        String contentDisposition, String mimeType,
        long contentLength) {

    DownloadManager.Request request = new DownloadManager.Request(
            Uri.parse(url));


    request.setMimeType(mimeType);


    String cookies = CookieManager.getInstance().getCookie(url);


    request.addRequestHeader("cookie", cookies);


    request.addRequestHeader("User-Agent", userAgent);


    request.setDescription("Downloading file...");


    request.setTitle(URLUtil.guessFileName(url, contentDisposition,
            mimeType));


    request.allowScanningByMediaScanner();


    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(
            Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                    url, contentDisposition, mimeType));
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    dm.enqueue(request);
    Toast.makeText(getApplicationContext(), "Downloading File",
            Toast.LENGTH_LONG).show();
}});
  • `I used below code and downloading and file naming is perfect.` Well.. then.. what do you want more if everything is perfect? – blackapps Mar 29 '22 at 10:34
  • `URLUtil.guessFileName( url, contentDisposition, mimeType)` We have no idea what that file name would become . You should have put a hardcoded file name there while seeking help on stackoverflow. – blackapps Mar 29 '22 at 10:37
  • @blackapps the file name is perfect but the file type is html and when i open the downloaded html page it show a page with error 404. I am downloading pdf files. – Dony Peter Puthumana Mar 29 '22 at 11:40
  • Mimetype? Filetype? Where are you talking about? – blackapps Mar 29 '22 at 12:27

0 Answers0