未提示下载 CSV 文件
我在 WordPress 中创建了一个自定义解决方案,它将生成一个 CSV 文件,通过单击直接链接到该文件的简单超链接即可下载。而不是提示将文件下载到计算机; CSV 在浏览器窗口中打开。
FWIW 我在 Media Temple 上使用 WordPress 的普通安装。
I've created a custom solution in WordPress that will generate a CSV file to be downloaded by clicking a simple hyperlink, linked directly to this file. Instead of being prompted to download the file to the computer; the CSV opens in the the browser window instead.
FWIW I'm on Media Temple using a vanilla install of WordPress.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发送正确的 mime 类型
并使用 Content-Disposition 标头告诉其下载: http:// www.jtricks.com/bits/content_disposition.html
您始终希望发送正确的 MIME 类型,否则防火墙、防病毒软件和某些浏览器可能会出现问题...
Send the proper mime type
And use the Content-Disposition header to tell it to download: http://www.jtricks.com/bits/content_disposition.html
You always want to send the proper mime type, otherwise firewalls, anti-virus software and some browsers may have issues with it...
您可以使用 PHP 的
header()
函数来更改 Content-type上面的代码将强制提示用户下载。其中
myFile.csv
应替换为您要下载的文件的路径。You can use PHP's
header()
function to change Content-typeThe above code will force a prompt to the user for download. where
myFile.csv
should be replaced with the path to the file you want downloaded.这有效:
另外,我个人不喜欢我网站上的链接,我喜欢按钮。如果您想要一个按钮来执行导出功能,您可以使用下面的代码。我只是想我会发布它,因为我第一次花了一些时间才弄清楚:)
This works:
Also, I personally do not like links on my sites, I like buttons. If you want a button to do for the export function you can use the code below. I just thought I would post it because it took me a bit to figure out the first time :)
您需要向浏览器发送
application/csv
的 MIME 类型,以便它将处理文件的责任转移到操作系统推荐(或用户选择)的任何位置。在 PHP 中(在将任何输出发送到客户端之前):
You need to send the browser a MIME type of
application/csv
so it will offload the responsibility of handling the file to whatever the OS recommends (or user chooses).In PHP (before any output is sent to the client):