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=
I got the answer thanks to Raghunandan for suggestion.
Here I need to extra call to get the header info of the downloading file. In the header section I got the name of the file.
Also I found this Filename from URL not containing filename suffix post through which I got more detail regarding header detail which we can getting at requesting time.
As we can use this URLUtil.guessFileName(url, null, null)
but this will given the file name of calling means for my case I have url like this
misc/dnload.php?t1=MzQ0MDA=&t2=MTY5NTUz&t3=MTY5NTUzMTMwMjEyMDNfcGhhcm1hY3kga2V5IGluZm8ucG5n&t4=MTMwMjEyMDM=
so from the above link this will extract the dnload.php as file name it not original file name which we download it just created download link for that file.
here is the code using this we can get the file name it just an extra call to get the information but actually we download, for download I have used android inbuilt api to DownloadManager to download file.
Content-Disposition this is the attribute in header section through which we can get the file name as in attachment form.
It'll will return info like this way Content-Disposition: attachment; filename="fname.ext"
so now just need to extract file name
class GetFileInfo extends AsyncTask<String, Integer, String>
{
protected String doInBackground(String... urls)
{
URL url;
String filename = null;
try {
url = new URL(urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
conn.setInstanceFollowRedirects(false);
String depo = conn.getHeaderField("Content-Disposition");
String depoSplit[] = depo.split("filename=");
filename = depoSplit[1].replace("filename=", "").replace("\"", "").trim();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
}
return filename;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute();
// use result as file name
}
}