How to get the file name which we uploaded on server when we provide linked for that file in page?
What I am doing is, I am providing data with file link in webview so whenever user click on link need to download from server as I have download this file from server but the problem is not able to get its exact type and name using DownloadManager. I want like this
See in above I have given link for my file in test_androt and when I click it will give the dailog with option having the file name, I don't know how to achieve this file name when user click on WebView URL link.
Edit Sorry to forgot mention that my URL look like this
misc/dnload.php?t1=MzQ0MDA=&t2=MTY5NTUz&t3=MTY5NTUzMTMwMjEyMDNfcGhhcm1hY3kga2V5IGluZm8ucG5n&t4=MTMwMjEyMDM=
You could simply parse the url and get the text after the last "/" and make that the file name, or you could use the following
URLUtil.guessFileName(url, contentDisposition, mimetype);
shown below as I have used it in my DownloadListener:
//this stuff goes inside your DownloadListener
@Override
public void onDownloadStart(final String url, String userAgent,final String contentDisposition, final String mimetype, long contentLength)
{
String fileName = URLUtil.guessFileName(url, contentDisposition, mimetype); //returns a string of the name of the file THE IMPORTANT PART
//proceed with download
}
If you don't want to use it inside your DownloadListener, you can use it without the contentDisposition or mimetype by just passing 'null' in for each parameter except the url (pass the url in) also.
EDIT: This only works if you have a url with a filename embedded in it. See Pratik's answer above if you are looking for a more foolproof way.