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

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

588 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