So I have made a little ajax request to my reviewsController@export.

Now when I console.log() the data in my success method, the ajax response shows the correct data. However my CSV has not downloaded. So I have all the right info and have created the csv essentially.

我认为这可能与设置标题有关,也许?

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();
}

Is there anything I am doing wrong here, or does Laravel have something to cater for this?

推荐答案

Try this version out - this should allow you to get a nice output using 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);
}

(Adapted from this SO answer: Use Laravel to Download table as CSV)

Try using a regular link with target="_blank" rather than using JavaScript/AJAX. Because it's a file download opening in a new tab, the user experience shouldn't be too clunky.

Laravel相关问答推荐

保存很多模型太慢

vagrant会损坏你的电脑. | macOS v12(蒙特雷)#13132

在 React 中将属性从一个组件传递到另一个组件

函数接受 Laravel 集合对象,尽管只允许使用字符串

laravel render() 方法有什么用?

Laravel/Eloquent:致命错误:在非对象上调用成员函数 connection()

如何在 laravel 5.2 中显示 500 内部服务器错误页面?

如何在使用 Laravel 在控制器中发送邮件之前更改邮件配置?

PHP 中的 Http 响应代码枚举

Laravel 5.5 类型错误:传递给 Illuminate\Auth\EloquentUserProvider::validateCredentials() 的参数 1 必须是实例

合并和排序两个 Eloquent 集合?

在 Laravel 中无效后防止重定向到主页

laravel 预期响应代码 250 但得到代码530

如何在中间件 Laravel 中获取请求的控制器和操作的名称

如何仅从 laravel FormRequest 中获取经过验证的数据?

当表中不存在列时如何将 paginate() 与 having() 子句一起使用

在 laravel 6.0 中未定义命令ui

Laravel assets资源与混合

将参数传递给过滤器 - Laravel 4

使用 Carbon 将小时转换为 PM 和 AM