raj
raj

Reputation:

How is the download file name chosen and is there a way to change it?

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?

Upvotes: 2

Views: 5849

Answers (4)

this. __curious_geek
this. __curious_geek

Reputation: 43217

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.

Upvotes: 0

Mark Biek
Mark Biek

Reputation: 150829

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.

Upvotes: 3

TheTXI
TheTXI

Reputation: 37905

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.

Upvotes: 2

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391496

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

Upvotes: 0

Related Questions