开发者

Make html/javascript link to a pdf only downloadable

开发者 https://www.devze.com 2023-03-18 03:55 出处:网络
How开发者_StackOverflow社区 can I link a pdf file on my web site only downloadable, and not display this PDF in the browser?The behavior of what to do with the content of an HTTP response is ultimatel

How开发者_StackOverflow社区 can I link a pdf file on my web site only downloadable, and not display this PDF in the browser?


The behavior of what to do with the content of an HTTP response is ultimately determined by the browser, not by the server. However, you can "suggest" to the browser that the content of the response is meant to be a "file" and suggest a name by which to save that file by using the content-disposition header.

Note that the HTTP protocol doesn't really transfer "files" in the sense in which most people infer. It transfers content with associated headers. So everything you do is done through those headers which accompany the content.

Edit: I just noticed your tags... Are you talking about serving the file for download or about linking to a file served elsewhere? If the latter then I'm pretty sure it's out of your control. Your JavaScript can maybe control the content of the request header, but the response header is entirely up to the server.


You can set the headers sent with the response to suggest to the the browser it should save the file:

 Content-disposition: attachment; filename=whatever.pdf

How you achieve this on a practical level depends on your webserver and/or programming environment. However, trying to get this done with JavaScript probably isn't the way forward.

Other options include tricking the browser into thinking the file isn't actually a PDF by setting the content-type to application/octet-stream or similar. This is commonly done, whether or not it's a good idea is debatable.

0

精彩评论

暂无评论...
验证码 换一张
取 消