php - 文件下载框
我给用户一个下载 csv 文件的链接...只需使用
Click <a href="report.csv">here</a> to download your file.
此文本和链接将显示在一个小弹出窗口中 - 高度 100 宽度 400。
如果用户单击“保存”-那么文件就没有问题保存到他们选择的任何位置
如果他们选择“打开”,则 csv 文件会显示在小弹出窗口中。这不是我想要的 - 我更喜欢 Excel 打开并在 Excel 中显示文件,甚至禁用“打开”按钮可能是一个可能的选择。
我有什么想法可以实现吗?
谢谢,
I'm giving a user a link to download a csv file ...just using
Click <a href="report.csv">here</a> to download your file.
This text and link is being displayed in a small popup window - height 100 width 400.
If the user clicks "save" - then no problem then the file is saved to wherever they choose
If though they choose "open" the csv file is then displayed in the small popup window. Which isn;t what I want - I'd prefer excel to open and display the file in excel, or even disabling the "open" button may be a possible option.
Any ideas how I can achieve either?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设您正在使用一些 javascript 打开该弹出框..所以我建议不要显示/打开您的小“下载”弹出框,而是在网页上显示此链接。因此,当有人单击链接时,会强制浏览器下载文件,如果他们希望打开该文件,他们可以并且可能会获得与浏览器窗口大小一样宽的视图。
// 例如,要强制下载 zip 文件,请将标头发送为
I assume that you are opening that popup box using some javascript .. so I'd suggest to dont show/open your tiny "download" popup box and instead show this link on a webpage. So when somebody clicks on the link force the browser to download the file and if they wish to open it they can and probably get a view which is as wide as of browswer's windows size.
// just for example, to force download of a zip file, send the headers as
您无权控制用户的设置,例如由哪个应用程序打开哪种文件类型。他们必须转到设置并更改文件关联。但是,请记住,并非每个人都安装了 Excel,因此强制用户在此特定应用程序中打开 csv 文件并不是一个好主意。
我也不认为你可以禁用“打开”选项,这也是由网络浏览器的配置决定的,无论它是否会显示窗口,或者是否会直接启动关联的应用程序等等......看来你已经不在了在你的具体情况下运气好。
You have no power over users' settings such as which file type is opened by which application. They would have to go to their settings and change the file association. However, bear in mind not everyone has Excel installed, so it's not a good idea to force the user to open the csv file in this particular application.
I also don't think you can disable the Open option, this is also dictated by the web browser's configuration whether it will or will not display the window or will it launch the associated application directly etc... it seems you're out of luck in your particular case.
我相信这是操作系统级别的并且无法更改。
I believe this is OS-level and can't be changed.
一种解决方案是将
target="_blank"
添加到标记中。这样,当用户选择“打开”选项时,至少该文件将在新的(全屏或至少可调整大小)窗口中打开。除此之外,您无法更改窗口的行为,如 ceejayoz 所说。
One half-solution is adding
target="_blank"
to the tag. That way, when users select the 'open' option, at least the file will be opened in a new (full-screen, or at least resizable) window.Other than that, you can't change the behaviour of the window, as ceejayoz stated.
标头应告诉浏览器将其加载到 Excel 中
header("缓存控制:maxage=1");
header("Pragma: public");
header("内容类型:application/vnd.ms-excel");
header("过期时间:0");
header("缓存控制:必须重新验证,后检查=0,预检查=0");
header("内容处置:附件;文件名=$csv_filename");
echo $csvString;
The headers should tell the browser to load it in excel
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=$csv_filename");
echo $csvString;