因此,我向我的reviewsController@export
提出了一个小小的ajax请求。
现在,当我在我的成功方法中console.log()
数据时,ajax响应会显示正确的数据。但是我的CSV还没有下载。因此,我有所有正确的信息,并创建了csv的本质。
我认为这可能与设置标题有关?
public function export()
{
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$reviews = Reviews::getReviewExport($this->hw->healthwatchID)->get();
$columns = array('ReviewID', 'Provider', 'Title', 'Review', 'Location', 'Created', 'Anonymous', 'Escalate', 'Rating', 'Name');
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach($reviews as $review) {
fputcsv($file, array($review->reviewID,$review->provider,$review->title,$review->review,$review->location,$review->review_created,$review->anon,$review->escalate,$review->rating,$review->name));
}
exit();
}
我在这里做错什么了吗,还是拉拉维尔有什么可供选择的?
发布于 2015-09-07 15:18:13
尝试这个版本--这将允许您使用Response::stream()
获得一个很好的输出。
public function export()
{
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=file.csv",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$reviews = Reviews::getReviewExport($this->hw->healthwatchID)->get();
$columns = array('ReviewID', 'Provider', 'Title', 'Review', 'Location', 'Created', 'Anonymous', 'Escalate', 'Rating', 'Name');
$callback = function() use ($reviews, $columns)
{
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach($reviews as $review) {
fputcsv($file, array($review->reviewID, $review->provider, $review->title, $review->review, $review->location, $review->review_created, $review->anon, $review->escalate, $review->rating, $review->name));
}
fclose($file);
};
return Response::stream($callback, 200, $headers);
}
(改编自这个答案:使用Laravel下载表格为CSV)
尝试使用与target="_blank"
的常规链接,而不是使用JavaScript/AJAX。因为它是一个新选项卡中的文件下载打开,用户体验不应该太笨重。
发布于 2019-03-06 22:53:11
我在Laravel 5.7的方法
/**
* @param array $columnNames
* @param array $rows
* @param string $fileName
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public static function getCsv($columnNames, $rows, $fileName = 'file.csv') {
$headers = [
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=" . $fileName,
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
];
$callback = function() use ($columnNames, $rows ) {
$file = fopen('php://output', 'w');
fputcsv($file, $columnNames);
foreach ($rows as $row) {
fputcsv($file, $row);
}
fclose($file);
};
return response()->stream($callback, 200, $headers);
}
public function someOtherControllerFunction() {
$rows = [['a','b','c'],[1,2,3]];//replace this with your own array of arrays
$columnNames = ['blah', 'yada', 'hmm'];//replace this with your own array of string column headers
return self::getCsv($columnNames, $rows);
}
发布于 2017-04-21 12:33:06
试试这个:
<?php
public function download()
{
$headers = [
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0'
, 'Content-type' => 'text/csv'
, 'Content-Disposition' => 'attachment; filename=galleries.csv'
, 'Expires' => '0'
, 'Pragma' => 'public'
];
$list = User::all()->toArray();
# add headers for each column in the CSV download
array_unshift($list, array_keys($list[0]));
$callback = function() use ($list)
{
$FH = fopen('php://output', 'w');
foreach ($list as $row) {
fputcsv($FH, $row);
}
fclose($FH);
};
return Response::stream($callback, 200, $headers); //use Illuminate\Support\Facades\Response;
}
注释:只有在不加载关系的情况下才能工作,否则就会产生异常
https://stackoverflow.com/questions/32441327
复制相似问题