web view download file from php server

1,662 views
Skip to first unread message

Chisan Gelu

unread,
Jun 15, 2022, 5:41:15 PM6/15/22
to DroidScript
Hi.I have an php app.I want to use it with webview component.The problem i have is that i can't download files  (.jpg....pdf) from app inside webview . In browser i dont have this problem !!!! Any Solution ???

Alan Hendry

unread,
Jun 16, 2022, 12:08:07 PM6/16/22
to DroidScript
HI,
To download a specific file then see https://symdstools.github.io/Docs/docs/app/DownloadFile.htm
If the user is browsing a site with webview and clicks on a link 
you could try to use onUrl callback to getUrl (check if it's a jpg/pdf/etc) and then download (I've never tried it).
Regards, ah

Chisan Gelu

unread,
Sep 10, 2022, 3:40:05 PM9/10/22
to DroidScript
Type of Files in WebView Android Studio?

If your website has a download option you really need to enable it from your Android WebView app. The Android java code below can be used to download any type of file to your Android device’s storage with the file’s original name, be it a .mp3, mp4, jpg, or pdf, the code will download any file that is downloadable. This code is for Android WebView, just paste it below in the oncreate method of WebView activity.




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





<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And also we must request user permission for devices. In the next article, We will tell you How to request user permission for devices in Android Studio




This is what i want in webview or html app.... 


Right2TheV0id

unread,
Sep 10, 2022, 6:54:36 PM9/10/22
to DroidScript
Did you try with  _AddPermissions("Storage")?

Chisan Gelu

unread,
Sep 11, 2022, 6:42:50 AM9/11/22
to DroidScript
In my app i have a form ...when i submit the form the server response with a image....   in browser work...but in droidscript give error ,,unsuccesfully download".....
  _AddPermissions("Storage")  ---- dont work....

Right2TheV0id

unread,
Sep 11, 2022, 11:58:38 AM9/11/22
to DroidScript
Are you able to download any file from your webview (like any spk from this forum)?
Maybe you should provide a minimal spk  (and a minimal php script) that reproduces your code.

Dave

unread,
Sep 11, 2022, 1:15:07 PM9/11/22
to DroidScript
Hi the DroidScript webview already supports the download of files via setDownloadListener.  

If your file urls do not have a normal file path in them but a query string instead, then you will need to set the contentDisposition header

Chisan Gelu

unread,
Sep 11, 2022, 1:52:38 PM9/11/22
to DroidScript
I use Laravel on web ...

 return response()->download(
                storage_path($fisier),
                $numef,
                [],
                'attachment'
            )->deleteFileAfterSend(true);;


In browser work fine but in webview ---- failed download


on webview only work if if i give a static url :

return "<a href=\"$fisier\" download id=\"d\">Download</a><script>document.getElementById(\"d\").click();history.back(); </script>";

Dave

unread,
Sep 11, 2022, 5:03:43 PM9/11/22
to DroidScript

Chisan Gelu

unread,
Sep 12, 2022, 10:53:10 AM9/12/22
to DroidScript
from the web 
"1

I have a similar problem to yours. The issue here is how WebView handles attachments (a real pain in the ass). When the WebView visits a web page that at some point returns an attachment, it says kinda like "Oh crap, what can I do with this non-HTML thingy?... Ey you! DownloadListener! do something with this URL that is saying some nonsense about an attachment". So, the DownloadListener takes over and here is the problem: it requests again the same URL to download the attachment, so, for downloading an attachment when visiting a page, the WebView perfoms 2 requests: the page itself and then another one to download the attachment, instead of just downloading it.


The second request performed by the DownloadListener will make another request to abc.com/xyz.php but this time it won't contain cookies or session information so it won't enter the "download" logic.

A possible solution would be to redirect to a temporal copy or the real path to the file that doesn't contains any logic so there is no problem"


Reply all
Reply to author
Forward
0 new messages