Automatic file transfer from HTML
-
20-08-2019 - |
Question
I'm building a web application that guides my users through the configuration and installation of an application. It builds a set of configuration files dynamically, then sends them in an archive (.ZIP file) along with an installer for the application. The web page is generated from a linux shell script (sorry), and for security reasons, I'd prefer the file be sent directly from the script, rather than as a link, so the user can't access it directly.
Here's the process: Once the user has entered some information, and the files have been generated, I want to display a page with instructions, then start the download automatically, without asking the user to click a "download this file" link:
#!/bin/bash
echo_header_and_instructions # Standard HTML
<Magic HTML tag to start transfer> # ??? What goes here???
command_to_stream_the_files # Probably 'cat'
echo_end_tags # End the page.
Thanks for your help!
Solution 3
A friend supplied the magic: use an <iframe>
tag to create an "invisible" frame that contains only the file to download:
<iframe id="file_download" width="0" height="0" scrolling="no"
frameborder="0" src=/cgi-bin/my/scripts/sendfiles?file=$filename.zip>
You need a browser that supports frames.
</iframe>`
OTHER TIPS
You Can't really do that I think.
You can force the file download through the following headers but as far is I know you can't mix HTML and file download.
Headers:
Content-type: MIME/type Content-Disposition: attachment; filename="archive.zip" Content-Length: filesize_in_bytes
The content length is not mandatory but using it will make sure that the download dialog box can show how much more file there is to download.
A thing you could do is reload the page using javascript. Because the page is a file download the original HTML page will stay in place:
<html> <head> <title>This is the page containing information</title> <script type="text/javascript"> window.onload = function(){ document.location = 'somefile.zip'; } </script> </head> <body> This page will stay visible while the file is being downloaded<br> </body> </html>
I think you can make the browser prompt the user to download a file by using the meta tag to do a refresh, but as Pim Jager said I don't think you can do it with one transfer. You could maybe try doing something like:
<meta http-equiv="refresh" content="5;url=http://example.com/pathtodownload.zip" />