how to set the filename for data that is sent to the browser  for downloading

Posted on 1998-09-29
Last Modified: 2013-12-25
I use a perl cgi script to limit access to files to authorized users only. I send the files with content-type application/octet-stream so that they will be offered for saving to disk. Unfortunately the filename that the browser is proposing is derived from the cgi scriptname. How can I set that filename in the cgi script ?
Question by:um3814
LVL 11

Accepted Solution

mouatts earned 100 total points
ID: 1828885
The way to do this is to ensure that the name of the cgi sccript is the same as the file that you wish to download.

Remember that CGIs don't have to have a .cgi, .pl, or .exe extension just about any extension can be used but how you implement this depends on your web server and platform.

On most servers on Unix platforms the inclusion of the shell in the first line is enough unless you have limited the executable types within the server configuration.

On NT boxes you will need to build an executable which can not have an extension of any other non-executable type. The exception to this is the Oracle Web Server wher you should use the PL/SQL cartridge and the UTL_FILE package to output the files.

Alternatively if you have and API (eg ISAPI, NSAPI) for the server you can normally utilise this to handle things by (for example) treating all requests for certain extensions as requests for downloads of similarly named files within a given directory)

If your application allows one of a number of files to be downloaded then you will need to include a bit of javascript that will change the action attribute on the form when a file is selected. (You can also do this at the point the form is submitted)
(The API appoach mentioned above resolves this problem without using Javascript.)

This approach does mean that you will need a number of instances of your CGI each with an appropriate name (they don't need to be in the same directory as the data file so the name clash will not be a problem). Obviously if you are on a UNIX platform then you can use a symbolic link so that only one actual copy exists.

Having said all it may also be worthwhile including a NAME attribute within either the <A> or <FORM> as I have just had a vague recollection that some browsers may use this instead of the HREF or URL when present.


Author Comment

ID: 1828886
I already supposed that it would be a tricky thing. I'm using Unix and I already know the method with symbolic links. As i cannot predict the name of the file, a solution is , to create the link and send a reload command for that link and then remove the link. But the link name must have the same name as the file
( incl. extension) and the extension will confuse the browser.
The content-type is not always the main criteria.
From all you said I learned , that the key point is to make the browser send an URL with the name of the file.


Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question