开发者

Opening files in browser instead of downloading

开发者 https://www.devze.com 2023-04-01 00:25 出处:网络
I have a href link to a pdf file that when clicked on it downloads. I want this to just open in a new page instead of downloading.

I have a href link to a pdf file that when clicked on it downloads. I want this to just open in a new page instead of downloading.

<a href="../file/1012">clicking here</a>

I've had a look at a website that does this w开发者_如何学Goith iframe, but I would prefer not to use that.


Your server should return this http header:

Content-Type: application/pdf

For viewing in browser it should also return:

Content-Disposition: inline;filename="myfile.pdf"

For downloading:

Content-Disposition: attachment;filename="myfile.pdf"


add target="_blank" to get a new window. adding .pdf may help. If you have control over the host web server adjusting the headers for the files in question will also help it open inline. I can tell you what headers to set if you are able too


The server tells the browser whether the content you are getting is "inline" or not. If you are controlling the server, you will want to change the server's settings for whether to serve the problematic file as an attachment. Otherwise, there are browser plug-ins which allow you to override this. For Firefox, there is Open in Browser; http://spasche.net/openinbrowser/


the answer by BornToCode is correct for "pdf".

for doc / docx / xls / xlsx / ...

and you don't want iframe, you can use:

<a href="http://docs.google.com/gview?url=https://sifr.in/img/292/1/courseAndroid.xlsx&embedded=true">Open your excel file</a>


You can make the link open in a new window by adding target="_blank". The question if the PDF file is opened in the window or downloaded depends on the software the user has installed, i don't think it is possible to control that.

0

精彩评论

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

关注公众号