tags:

views:

290

answers:

4

I have a link to a file not on my server:

<a href="history.nasa.gov/monograph15b.pdf">NASA</a>

I would like to make it so that when a visitor clicks on the link to download, the name that would pop up for the PDF would be NASA.pdf, and not monograph15b.pdf.

Is this possible in any language? Thanks

+2  A: 

The only way to really do that is to first download the file in question onto your own server, name it as you wish, and then serve it back up to your end users.

TheTXI
If you host the file, you can specify the save file name as a different name in the HTTP header
Matthias Wandel
Matthias: based on the original poster, it's apparent he's not hosting the file himself so changing headers is not going to help him, other than performing that instead of changing the filename during his own download process.
TheTXI
@matthias, yea, it's on a different server
A: 

Look into the HTTP header Content-Disposition header, it will allow you to specify the filename.

Lasse V. Karlsen
Is that something that could be used for remote files which is what raj seems to be pointing at?
TheTXI
No, you need to funnel the download through some part of your site that can change the HTTP headers on their way, either a web page that changes the headers, and then serves, the file, or you need a handler that will change the download on its way.
Lasse V. Karlsen
And I just love drive-by downvotes that are done without reason.
Lasse V. Karlsen
+2  A: 

If you aren't going to host the file yourself, you may be able to read in the file contents and then send those contents directly to the browser with the appropriate headers.

Note that this will be slow because you'll be re-downloading the file from NASA each time your PHP page runs.

<?php
$filename = "http://history.nasa.gov/monograph15b.pdf";
$outputfilename = "NASA.pdf";

header("Content-Type:  application/pdf");
header("Content-Disposition:  attachment; filename=\"" . basename($outputfilename) . "\";" );
header("Content-Transfer-Encoding:  binary");
readfile("$filename");
?>

This approach also requires that PHP be configured so that fopen() can handle reading a file over HTTP.

Mark Biek
thanks, this definitely works.Do you know if something like this is possible client side - if for example, the server doesn't have access to a pdf, but the user does? Maybe in javascript?
I'm not sure I follow. Can you explain a little more?
Mark Biek
for example, nasa has a few pdf's that can only be accessed if the user is logged in. I presume the script downloads the pdf and then sends it to the user. In this case, the script wouldn't have access to the pdf as it isn't logged in to NASA's servers.Thanks for the help!
Hmmm. I can't think of a good way to do that off the top of my head.
Mark Biek
A: 

You need to use any server side technology like ASP.NET. Where in you put in a button on the page. When the button is clicked it will perform following tasks.

1.) Response.Clear();

2.) Response.AddHeader("Content-Disposition", "attachment; filename=Nasa.pdf");

3.) Response.ContentType = "application/octet-stream";

4.) Response.WriteFile(context.Server.MapPath("history.nasa.gov/monograph15b.pdf")); // you need to set the realtively correct path for your application.

this. __curious_geek
thanks! it seems like this is a similar algorithm to mark's suggestion