【问题标题】:CodeIgniter 4 - Open file in browser instead of downloadCodeIgniter 4 - 在浏览器中打开文件而不是下载
【发布时间】:2020-11-03 13:23:28
【问题描述】:

我有以下创建超链接的代码:

<div class="col-md-12">
    {% set href = 'customers/outputfile/' ~ doc.ICDU_GUID %}
    <a href="{{ href | _base_url }}" target="_blank">{{ docName }}</a>
</div>

生成的 URL 的示例是 localhost:8080/customers/outputfile/b41b055e-91ad-4dc8-8184-9cb910cb3e02

在控制器中,我获取文档的位置,然后返回响应:

public function outputFile($id) {
    $dbCustomersModel = new DB_CustomersModel();
    $filePath = $dbCustomersModel->getFilepathFromId($id);

    return $this->response->download($filePath->ICDU_DocLocation, null);
}

当我单击超链接时,会打开一个新选项卡,但会立即关闭,并出现文件下载弹出窗口。

我想要的是让文件在新标签页中打开,而不是被要求下载。文件可以是图片、pdf等...

【问题讨论】:

标签: codeigniter codeigniter-4


【解决方案1】:

通过使用

return $this->response->download($filePath->ICDU_DocLocation, null);

您正在强制浏览器下载文件,而不是尝试直接读取它。为了增加文件被查看而不是下载的机会,请添加标题

Content-Type: application/pdf - 或其他合适的类型 Content-Disposition: inline;filename="myfile.pdf" - 用于尝试调用浏览器打开它

带有响应的 Codeigniter 4:

$response->setHeader('Content-Type', 'application/pdf')
         ->setHeader('Content-Disposition', 'inline;filename="myfile.pdf');

来源:Opening files in browser instead of downloading

【讨论】:

    猜你喜欢
    • 2011-11-08
    • 2019-03-08
    • 2013-10-25
    • 1970-01-01
    • 2016-12-14
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多