Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

强制浏览器下载文件而不是查看文件内容 #100

Closed
Dream4ever opened this issue Oct 27, 2020 · 0 comments
Closed

强制浏览器下载文件而不是查看文件内容 #100

Dream4ever opened this issue Oct 27, 2020 · 0 comments
Labels
Browser All about browsers Front-end Everything you see and experience

Comments

@Dream4ever
Copy link
Owner

问题描述

公司的一个网站上,放了一些文件供用户下载。最近有用户反映在 QQ 浏览器中点击 PPT 文件,不是下载文件,而是直接在浏览器中显示 PPT 的内容。

问题解决

上网查了一下 QQ 浏览器官网的功能介绍,原来是自带 Office 文档的查看功能。要解决这个问题,就需要让浏览器强制下载 Office 文档,而不是在浏览器中直接打开,就像浏览器对 PDF 的处理那样。

上网查了一下,在 Stack Overflow 的一篇问答 Force to open “Save As…” popup open at text link click for PDF in HTML 中,提到要给 a 标签增加 download 属性来强制下载文件,另外最好再加上 target="_blank" 这么一条属性,来尽量保证下载功能的正常。

实际测试了一下,加上这两个属性之后,QQ 浏览器果然能正常下载了,问题解决。

PS:由于网站上的资源只想让注册/登录用户下载,而给 a 标签添加 href 属性之后,默认情况下点击 a 标签会立刻下载 href 属性中的文件。因此为了在特定情况下禁止弹出下载对话框,还需要在 a 标签的 click 事件中执行 e.preventDefault()

参考资料

@Dream4ever Dream4ever added Front-end Everything you see and experience Browser All about browsers labels Oct 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Browser All about browsers Front-end Everything you see and experience
Projects
None yet
Development

No branches or pull requests

1 participant