Laravel 5.1 中的数组到 CSV 文件下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43683337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:52:29  来源:igfitidea点击:

Array to CSV file download in Laravel 5.1

phplaravelcsvresponse-headersdownloadfile

提问by Rajat

I am having an array and i am trying to download it as csv file my array looks like this

我有一个数组,我正在尝试将其下载为 csv 文件,我的数组如下所示

Array(
[0] => Array
    (
        [Date] => 2017-01-02
        [Name] => Asia consumer MC Alpha bmk - risk
        [Level] => .333000
    )

[1] => Array
    (
        [Date] => 2017-01-03
        [Name] => Asia consumer MC Alpha bmk - risk
        [Level] => .333000
    ))

my code to convert the array to file is

我将数组转换为文件的代码是

public function DownloadFile($filetype,$Array_data,$content){

    $LevelArray = array('Date','Name','Level');

    $selected_array =  $LevelArray;
    if($filetype =='csv'){

            $Filename ='Level.csv';
            header('Content-Type: text/csv; charset=utf-8');                
            header('Content-Disposition: attachment; 
            filename='.$Filename.'');
            // create a file pointer connected to the output stream
            $output = fopen('php://output', 'w');
            fputcsv($output, $selected_array);
            foreach ($Array_data as $row){
                fputcsv($output, $row);
            }
            fclose($output);                
        }}

in response i am getting the output on response but that response is not converted into a file type though headers are pointing to get the csv file from it.

作为响应,我得到了响应的输出,但该响应并未转换为文件类型,尽管标头指向从中获取 csv 文件。

 Date,Name,Level
2017-01-02,"Asia consumer MC Alpha bmk - risk",.333000
2017-01-03,"Asia consumer MC Alpha bmk - risk",.333000
2017-01-04,"Asia consumer MC Alpha bmk - risk",.333000

I have tried using response method of laravel and many others too nothings seems to work, its headers not able to force browser to download the file

我已经尝试使用 laravel 和许多其他的响应方法似乎也没什么用,它的标题无法强制浏览器下载文件

RESPONSE Headers look like this

响应标题看起来像这样

Connection:close
Content-Disposition:attachment; filename=Benchmark-Level.csv
Content-type:text/csv;charset=UTF-8
Host:localhost:8000
X-Powered-By:PHP/5.6.25

回答by waterloomatt

Is your 2nd header split over 2 lines? It needs to be a single line with no line breaks.

您的第二个标题是否分为 2 行?它必须是单行,没有换行符。

header('Content-Disposition: attachment; filename='.$Filename.'');

Edit

编辑

This code is working for me. I added one more header to force download, but it works in Chrome with or without that extra header. Could you please try running it in different browsers? My thought is that there are browser settings preventing the download.

这段代码对我有用。我又添加了一个标题来强制下载,但无论有没有额外的标题,它都可以在 Chrome 中使用。您可以尝试在不同的浏览器中运行它吗?我的想法是浏览器设置阻止下载。

<?php

$selected_array = array('header:col1','header:col2', 'header:col3');

$Array_data = array(
    array('row1:col1','row1:col2', 'row1:col3'),
    array('row2:col1','row2:col2', 'row2:col3'),
    array('row3:col1','row3:col2', 'row3:col3'),
);

$Filename ='Level.csv';
header('Content-Type: text/csv; charset=utf-8');
Header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$Filename.'');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
fputcsv($output, $selected_array);
foreach ($Array_data as $row){
    fputcsv($output, $row);
}
fclose($output);
?>