提示用户下载PDF文件而不是打开

Jit*_*yas 7 javascript php apache

在我的项目网站中,如果单击链接,PDF将在新窗口或父窗口中打开.好吧,我想要一个框出现,提示用户下载文件而不是打开它.

有没有人知道在所有浏览器中使用默认设置执行此操作的简单JavaScript onClick事件?

我的服务器是基于PHP的.

Dav*_*und 13

由于您的编辑声明您正在使用PHP,因此以下是如何在PHP中执行相同的操作:

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
Run Code Online (Sandbox Code Playgroud)


Dav*_*und 9

既然你已经将它标记为.NET,我会说这是你最好的解决方案:

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();
Run Code Online (Sandbox Code Playgroud)