Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


link to a cgi problem

Posted on 1999-06-29
Medium Priority
Last Modified: 2013-12-20
I have a link to a CGI.
this CGI queries a database and returns its result as text.
this text needs to be saved as a file localy.
I want that the CGI responce will determine the file's name and extention that will be written in the "file save as" dialog.
Question by:jurbin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1834817
In order to make the Save As box pop up, you need to tell the browser that the content is actually a different type.  For example:

print "Content-type: application/octet-stream\n\n";

print <<EOFILE

I am not aware of any standard way to set the default filename for the box that works for all browsers; you may be able to find a non-standard way that works on some browsers, but not all.

Expert Comment

ID: 1834818
The name that will be offered by default is the name of the page that was requested so if you want to have that other than the name of the cgi you will need to have the cgi return a page that contains a reference to the required file name and have the file name created on the server.  I use some JS in my pages of the form:
The cgi has already created the file text.fil


Expert Comment

ID: 1834819
also - there is a missing ; in the answer above

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.


Expert Comment

ID: 1834820
If the extension is something like .txt, won't this solution just load the file in the browser rather than presenting a Save As box?

I'm not sure what jurbin is using his script for, but it if the script receives a lot of traffic and the filename is always the same, separate processes would run the risk of overwriting the generated text file before the original process is done with it...  You could prevent this by embedding the Process ID number (PID) in the filename, if necessary.  You would also need some way of cleaning up the file(s) after they were no longer needed.

I think the best solution depends on what this script is to be used for and how often it will be run.

Author Comment

ID: 1834821
- the big broblem is to return the result to the client WITHOUT creating a file on the server.
- the result must be saved on the client side when the file name is made in this format ddmmDDMM.ext (where ext can be 1 of 8 different predefined extentions. so some clients can have the same file name (but never the same data).

LVL 11

Expert Comment

ID: 1834822
I think that you will find that the answer you want has actually been given although its a bit disjointed.

Firstly to force the browser to use the correct file name you CGI must be called this. So for example if it was a comma seperated file that you where downloading (ie .csv) your CGI must be called ddmmDDMM.csv. This CGI should output the data in the approriate format directly.

Secondly to ensure that the browser sees the files as its correct type you must return the appropriate content-type. This content type is about the first thing that the CGI must output and would take the form (in this example)

Content-type: application/x-csv

Using a different extension to the standard one is not a problem on a UNIX box so long as the directory and CGI Permissions are correct.

On IIS it is a bit trickier. If the CGI is in fact an ASP page then you can associate the extension with the asp.dll within the application configuration screen. If it is an executable however I have never found a way to do this with IIS.


Accepted Solution

pru2 earned 800 total points
ID: 1834823
I don't know what language you are using, but I'll assume you know how to create the HTTP response headers.

You need to create a the header :

Content-Disposition: inline; filename="sample.doc"

where sample.doc is the name of the file you want to appear in the save dialog box.

Please note : this header is only recognized by Netscape, and not IE.

To the best of my knowledge there is no way of doing this in IE.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Shoutout to Emily Plummer ( for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 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