CSV文件在浏览器中打开,而不是提示DL [英] CSV File Opening Inline In Browser Rather Than Prompting DL

查看:375
本文介绍了CSV文件在浏览器中打开,而不是提示DL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个php文件,创建一个csv文件fopen,fputcsv,然后fclose和一个javascript函数的导出按钮,指示窗口打开所述csv文件。 csv文件正确创建,但当我点击导出按钮,它只是打开csv文件在另一个选项卡,只有原始数据。我正在寻找(和期待)是提示下载或打开文件(然后我会用excel打开)。如果我保存的页面,现在打开它在excel中打开,但它有来自html(我不想要的)的标记,所以我只是试图看看是否有任何特别的,可以解决这个问题。我已经在ie9和firefox测试了它。

So I have a php file that creates a csv file with fopen, fputcsv, then fclose and a javascript function for an export button that directs the window to open said csv file. The csv file gets created correctly but when I hit the export button it just opens the csv file in another tab with just the raw data. What I was looking for (and expecting) was it to prompt a download or opening of the file (which I would then open with excel). If I save the page that gets opened now it opens fine in excel but it has the markup from the html (which I don't want) so I'm just trying to see if there is anything in particular that could fix this issue. I have so far tested it in both ie9 and firefox.

谢谢!

推荐答案

您在发送输出之前发送了相应的标题吗?

Have you sent an appropriate header before sending the output?

ob_clean();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="some_filename_' . date('Ymd') . '.csv"');

这篇关于CSV文件在浏览器中打开,而不是提示DL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆