【问题标题】:have Restler method return a Content-Disposition header让 Restler 方法返回一个 Content-Disposition 标头
【发布时间】:2015-04-10 05:52:29
【问题描述】:

在我的一种 get 方法中是否可以指定特定的 Content-Type 和 Content-Disposition?取回我的数据后,我正在这样做:

return Scope::get('CsvFormat')->encode($data);

因此返回的数据是 CSV 格式。现在我想将内容类型设置为 text/csv 并设置一个 Content-Disposition 以便它实际上允许文件下载,而不仅仅是显示内容。

【问题讨论】:

  • 请问具体目的是什么?我可能有更好的解决方案!
  • 服务器后面有一个数据库,其中包含当月的数据,客户端想要网页上的一个按钮,将数据下载为 XLS 文件。我的想法是 get 方法会调用存储过程来获取数据,将其转换为 CSV,然后将其作为文件发送回来,这样当他们点击按钮时,他们会看到“另存为”对话框excel”文件。

标签: php restler


【解决方案1】:

这是一个工作示例!

Excel.php

<?php

class Excel
{
    /**
     * Download CSV data which can be rendered with Excel
     *
     * @return array
     * @format CsvFormat
     * @header Content-Disposition: attachment; filename="excel.csv"
     */
    public function download()
    {
        //csv compatible array
        $data = [['name' => 'John Doe', 'age' => '23'], ['name' => 'Mika', 'age' => '45']];

        return $data;
    }
}

你的index.php

<?php

require '../vendor/autoload.php';

use Luracast\Restler\Restler;

$r = new Restler();
$r->addAPIClass('Excel');

$r->setOverridingFormats('CsvFormat');
$r->handle();

试试http://localhost/excel/download

【讨论】:

    猜你喜欢
    • 2012-02-27
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2014-02-03
    • 2011-09-27
    • 2018-10-30
    • 2017-10-29
    相关资源
    最近更新 更多