Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

downloadable page link

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
what is the code to create a link which when clicked brings up the save as screen (for example when you are downloading something). I dont want a form button but a link of some-kind. for example i have this "csv" file that i have put up as a link " - when users click on the link or try to go to this page i want to bring up the "save as" screen to choose the location where to save this file.
thanks
ko12
 
you just use the standard link, if there are no browser helper objects defined for the browser then it will give you that dialog. Otherwise it will perform wahtever action it is coded to do e.g. open an instance of Excel in the browser and the file in there.
 
This is something that you have to either instruct your users to do (with some instructinal text on the page -- remember to handle computer platforms that don't have a right mouse button), or you have to rely on it being a server-side solution.

The server-side solution requires you manage the headers being sent to the page. When the user clicks the link (to download the file you want them to have) the server interprets the request and delivers specific headers before delivering the content requested. The browser reads these headers and this is how the "Save as" popup appears sometimes when you go to download a file.

Are you using a server-side solution like PHP for your pages? Let us know and I can find some code that delivers a CSV and forces a popup "Save as" to appear (in PHP).

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
hi,
we are using server side solution - for sure the code is in php
ko12
 
I have created this single PHP file to show how you can use specific header information to force a browser to handle downloading/prompting for a location when recieving a file from a web server.

The code below is confirmed to work perfectly in:
IE 6, FF 1.06, Opera 7.54, Opera 8.0 (all on Windows)
IE 5.23, FF 1.04, Safari 1.31, Netscape 7.1 (all on MacOSX)

I noticed that using Netscape 7.2 for Windows, that it appended .php to the file name. I can find no way to change this at this stage. Surprisingly this is not present when using the MacOSX version that I have.

In this instance I am creating a simple CSV file that is being delivered to the user with this mechanism.
Code:
<?
// set up the data to be exported as a CSV file
$my_data = '"Name","Phone Number","Age"';
$my_data .= "\n";
$my_data .= '"Jane Doe","555-123-4567",27';
$my_data .= "\n";
$my_data .= '"John Smith","555-123-5678",25';
$my_data .= "\n";
$my_data .= '"Samuel Smith","555-123-6789",32';
$my_data .= "\n";

// set up the file name for the exported file
$output_file = 'MyAddressBook.csv';

// send the headers off
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $output_file . '"');

// send the data
echo $my_data;

// stop all further processing
exit();
?>
I have also uploaded the exact code above to the following URL that you can use to test it:


Hope that gets you going!
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Presumably, Jeff, you could tweak that php program so it works as a wrapper - reading a csv file from elsewhere on the server and echoing its content with the relevant headers prepended. I know I could do that with perl, maybe I'll have a go later...

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Most definately, Chris. I think in php it's a simple couple of lines to do it (as I'm sure it will be in almost any server-side code).

I'd be keen to see if anyone is able to improve the header/compatibility with Netscape on Windows, though... it's something I'm sure many of us would find useful from time to time.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Here's my version. It works fine in FF, but the Internet Exploder seems to ignore the MIME headers and display it regardless...
Code:
#!/usr/bin/perl                                                     
                                                                    
# Location of the source data files, relative to the root directory 
# ending in a trailing slash. This can be outside your web space.   
                                                                    
$DATA_DIR = "/some/path/here/";  
                                                                    
                                                                    
if ($ENV{'PATH_INFO'} =~ /([^\/]*)$/) {                             
   $file = $1;                                                      
}                                                                   
$path="$DATA_DIR$file";                                             
                                                                    
if ( -f $path && -r $path ) {                                       
   # Add other headers in here if you need/want them                
   print "Content-Transfer-Encoding: none\n";                       
   print "Content-Dispostion: attachment; filename=$file;\n";       
   print "Content-Type: application/octet-stream\n\n";              
   open(FILE, $path);                                               
   print <FILE>;                                                    
   close(FILE);                                                     
} else {                                                            
   print "Status: 404 Not Found\n\n";                               
}
Save this in your cgi-bin as, say, [tt]download.pl[/tt] and you can reference any file in the data directory like this:
Code:
<a href="/cgi-bin/download.pl/myfile.csv">Downoad file</a>



-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top