在我的项目网站中,如果我点击链接,PDF会在新窗口或父窗口中打开。我希望出现一个提示框,提示用户下载文件,而不是打开它。
有没有人知道一个简单的JavaScript onClick事件,在所有浏览器中,使用默认设置实现此功能?
我的服务器是基于PHP的。
有没有人知道一个简单的JavaScript onClick事件,在所有浏览器中,使用默认设置实现此功能?
我的服务器是基于PHP的。
由于您的编辑说明您正在使用PHP,因此以下是如何在PHP中执行相同操作的方法:
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
既然你标记了.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();
Response.ContentType = "application/octet-stream";
Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Content-disposition: attachment; filename=' . basename($file));
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file)); // provide file size
header('Connection: close');
readfile($file);
$file
是文件的完整路径或 URL。
header('Content-Length: 12345');
- Justin Jenkins