Download in webview

I want to download a file using a webview. A PDF file in particular. Is this at all possible using a webview? Because all my attempts so far have failed. All I get is a “loaded” event from the webview. But no data or file is downloaded.

I know how to download files using the network.* API, but since the downloadable files are behind a login session and are generated on request, they can, in general, only be reached via a web browser session.

Any ideas would be helpful. I made a test page with several links to a PDF, but none of them can be used for downloading a file within a webview. Here’s the link: https://www.flexuur.com/download.php

Here’s the test code I’ve been using to check if download works. Disregard the “idebug” mention. This is a helper class that contains functions to print out complete contents of a lua table for debugging purposes. 

local dbg = require ('idebug') local webview = native.newWebView(display.contentCenterX,display.contentCenterY, display.contentWidth, display.contentHeight) local function webListener( event ) dbg.print\_r( event ) if event.url then print( "You are visiting: " .. event.url ) end if event.type then print( "The event.type is " .. event.type ) -- print the type of request end if event.errorCode then native.showAlert( "Error!", event.errorMessage, { "OK" } ) end end webview:addEventListener( "urlRequest", webListener ) webview:request( "https://www.flexuur.com/download.php" )

Thanks in advance for your help.

I know of no way you can do this. Corona’s implementation of webviews is extremely limited; you can’t add overlays for translucent navigation buttons, resize (so headers can seamlessly appear or disappear when scrolling), handle orientation changes properly, or download files. If Corona has not added the functionality you need in ~5 years, they are not likely to do so anytime soon.

(Warning: Horrible hack follows.) Corona does let you see what links users click. So you could encode a small PDF file in a URL, grab the URL when a button on the website is clicked, parse the URL, write the URL to local storage as a PDF, and show the PDF. Enjoy. :slight_smile:

Thanks for your reply. :slight_smile:

I wish I could do the “hack” as you mentioned, but unfortunately the actual PDF’s are available in a PHP session only, behind a username and password. So downloading them with a socket connection does not work. I tried doing a login using network.request calls but I could not emulate a submission of the login form because of some strange bug (multipart/formdata does not work flawlessly on Android). Anyway, I’m a bit stuck.

Cheers.

~Rob

After reading this again, I now understand what you’re saying. And this is a hack indeed :-) 

I think the webdev-team will most likely not agree in developing such a workaround, hahaha. But maybe if I bring them some kitkat’s who knows :wink:

I know of no way you can do this. Corona’s implementation of webviews is extremely limited; you can’t add overlays for translucent navigation buttons, resize (so headers can seamlessly appear or disappear when scrolling), handle orientation changes properly, or download files. If Corona has not added the functionality you need in ~5 years, they are not likely to do so anytime soon.

(Warning: Horrible hack follows.) Corona does let you see what links users click. So you could encode a small PDF file in a URL, grab the URL when a button on the website is clicked, parse the URL, write the URL to local storage as a PDF, and show the PDF. Enjoy. :slight_smile:

Thanks for your reply. :slight_smile:

I wish I could do the “hack” as you mentioned, but unfortunately the actual PDF’s are available in a PHP session only, behind a username and password. So downloading them with a socket connection does not work. I tried doing a login using network.request calls but I could not emulate a submission of the login form because of some strange bug (multipart/formdata does not work flawlessly on Android). Anyway, I’m a bit stuck.

Cheers.

~Rob

After reading this again, I now understand what you’re saying. And this is a hack indeed :-) 

I think the webdev-team will most likely not agree in developing such a workaround, hahaha. But maybe if I bring them some kitkat’s who knows :wink: