Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top