Reputation: 75
I have a webview app, and it's for a my local website So..
When I click on a link to download any file, it's download it but with "filename" name just like what I choose it in my code !.
So is there any way to get the file name that downloading to set it as the name of download .
Please edit my code and past it for me because I am a beggener in android programing .
This is my DownloadListener code
Thank's every one ^_^.
webview.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url,String userAgent,String contentDisposition,String type,long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "File Name");
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (manager != null) {
manager.enqueue(request);
Toast.makeText(getApplicationContext(), "بدأ التحميل ...", Toast.LENGTH_LONG).show();
}
}
});
}
Upvotes: 0
Views: 1002
Reputation: 75
I find the code that do what I need, Thank's for every one
I hope to help some one .
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);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE!!------------------------
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("بدأ تحميل الملف...");
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(), "بدأ تحميل الملف...", Toast.LENGTH_LONG).show();
}
});
Upvotes: 0
Reputation: 176
If it is Get url.the filename will be the end of url.
If it is POST method. the filename will be Agreed in response head.
hope to help you.
Upvotes: 1
Reputation: 190
Did you try setting the filename in Content-Disposition header? https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
Something like request.addRequestHeader("Content-Disposition", "attachment; filename=\"filename.txt\"");
Upvotes: 1