强制下载CSV文件 [英] Force Download CSV File
问题描述
我试图创建一个按钮,将强制CSV文件下载,但由于某种原因,我不能得到它的工作。这是我有的代码:
I am trying to create a button that will force a CSV file download but for some reason I am not able to get it working. This is the code that I have:
public function export_to_csv($result) {
$shtml="";
for ($i=0; $i<count($result); $i++){
$shtml = $shtml.$result[$i]["order_number"]. "," .$result[$i]["first_name"]. "," .$result[$i]["middle_name"]. "," .$result[$i]["last_name"]. "," .$result[$i]["email"]. "," .$result[$i]["shipment_name"]. "," .$result[$i]["payment_name"]. "," .$result[$i]["created_on"]. "," .$result[$i]["modified_on"]. "," .$result[$i]["order_status"]. "," .$result[$i]["order_total"]. "," .$result[$i]["virtuemart_order_id"]. "\n";
}
Header('Content-Description: File Transfer');
Header('Content-Type: application/force-download');
Header('Content-Disposition: attachment; filename=pedidos.csv');
}
$结果变量来自此处:
The $result variable comes from here:
public function get_orders_k() {
$db=JFactory::getDBO();
$query = " select o.order_number, o.virtuemart_order_id, o.order_total, o.order_status, o.created_on, o.modified_on, u.first_name,u.middle_name,u.last_name "
.',u.email, pm.payment_name, vsxlang.shipment_name ' .
$from = $this->getOrdersListQuery();
$db->setQuery($query);
$result=$db->loadAssocList();
if ( $result > 0 )
{
$datos = VirtueMartModelKiala::export_to_csv($result);
}else return 0;
}
我不知道哪里开始看。我看过互联网上的各种方式这样做,尝试所有的人,仍然不能设法使其工作。请帮助我!
Im not sure even where to start looking. I have look over the internet on various ways of doing this and tried all of them and still can't manage to get it working. Please help me out!
- 谢谢
推荐答案
/ p>
Put this code below your loop.
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="pedidos.csv"');
echo $shtml;
您是如何找到内容类型应用程序/ force-download的?我从来没有听说过。 CSV的正确MIME类型是 text / csv
How did you find out the content type application/force-download? I've never heard of it. The proper MIME type for CSV is text/csv
您可以找到更多示例如何使用 php手册
You can find more examples how to use header function in php manual
您还需要显示 $ shtml
你没有在你的代码。
You need also display $shtml
you didn't do it in your code.
这篇关于强制下载CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!