views:

653

answers:

3

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!

+3  A: 

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>
Pim Jager
Thanks for the example--I do it that way now, but it requires an extra click from the user. I'd like to emulate, say, SourceForge: when you choose to d/l it displays a new page, then starts the d/l automatically. How's that happen???
Adam Liss
Maybe simulate click using Javascript? I'll update answer.
Pim Jager
Thanks, Pim. Don't spend too much time, as I found a workable solution (see below), but I appreciate your help--I learned a lot!
Adam Liss
Okay, no problem, that's why I'm here. (I Learned a lot too. Half the answer I give were things I didn't knew myself either)
Pim Jager
+1  A: 

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" />
Tom Haigh
A: 

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>`
Adam Liss
You should place the 'You need a....' tag inside <noframes>You need a...</noframes>
Pim Jager
@Pim: The iframe tag does this for you. See http://www.w3schools.com/tags/tag_iframe.asp
Adam Liss