views:

251

answers:

5

in my project site if i click on a link and the PDF opens in a new or parent window. Well I want a box to appear that prompts the user to download the file instead of opening it. Does anyone know of a simple JavaScript onClick event that will do this? In all browser default with default settings.

EDIT: sorry my server was PHP based

+1  A: 

You can't do it via javascript, you need server side implementation.

Here's the SO Post which should help:
Allowing user to download from my site through Response.WriteFile()

o.k.w
+9  A: 

Since you've tagged it .NET, i'd say this is your best solution:

    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment;filename=download.pdf");
    Response.WriteFile(Server.MapPath("~/files/myFile.pdf"));
    Response.Flush();
    Response.Close();
David Hedlund
+1  A: 

http://aspalliance.com/259%5FDownloading%5FFiles%5F%5FForcing%5Fthe%5FFile%5FDownload%5FDialog

Gregoire
the required code from the article: Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
just somebody
+3  A: 

Change the Content-Type to application/octet-stream. You may find however, that some browsers will infer from the file extension that it should open as a PDF with your favorite PDF viewer.

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

Also, set the following:

Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
ck
I need for all pdf on a site
metal-gear-solid
+1  A: 

Since you're edit states that you're using PHP, here's how to do the same in PHP:

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
David Hedlund
i want to enable this thing for all pdf on site not for any specfic pdf
metal-gear-solid