迫使浏览器下载文件,而不是观看它 [英] force browser to download a file instead viewing it

查看:101
本文介绍了迫使浏览器下载文件,而不是观看它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站我想有两个按钮 - 第一个下载应该下载一个PDF到用户的计算机和另一查看,在一个新的选项卡中打开PDF文件。问题是,我不知道如何使下载按钮。你可能会说,这是没用的,你可以在查看后保存的PDF,但我已经做出了显卡这样的,真的不希望去改变它。是否有可能prevent从浏览器中打开PDF文件,并​​使其下载文件呢?

On my web I want to have two buttons - first Download that should download a pdf to the user's computer and another View that opens pdf in a new tab. The thing is, I don't know how to make the Download button. You might say that it is useless as you can save pdf after "viewing" it but I have already made graphics like that and don't really want to change it. Is it possible to prevent browser from opening the pdf file and make it download the file instead?

推荐答案

您需要强制文件下载在你的服务器端脚本:

You need to force file download in your server-side script:

下面是一个PHP的例子:

Here is a PHP example:

$file = "path/to/my-file.pdf";

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");

这篇关于迫使浏览器下载文件,而不是观看它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆