需要实现点击button然后获取信息,将内容以文件形式下载下来。
但是现在点击button是打开新窗口并且展示出内容,而不是弹出下载窗。
代码如下:
$filename = $this->input->cookie('merchant_id'); $timestamp = date("YmdHis",time()); $filename .= $timestamp.'.'.$fileType; $title = mb_convert_encoding($title,'gbk','utf-8'); header('Content-type: application/octet-stream'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Content-Disposition: attachment; filename=' . $filename); echo $title; $ret = $this->download_model->QueryList($Req, $Resp); $content = $this->GetDownLoadContentNew($ret['bill_list'], $fileType); echo $content;
其中GetDownLoadContentNew是对内容进行相应的格式化。
不知道哪里有问题或者有什么地方没注意到?希望大家帮忙看下,谢谢。(fileType是csv或txt,在不同浏览器上都是一样都无法触发下载)
需要实现点击button然后获取信息,将内容以文件形式下载下来。
但是现在点击button是打开新窗口并且展示出内容,而不是弹出下载窗。
代码如下:
$filename = $this->input->cookie('merchant_id'); $timestamp = date("YmdHis",time()); $filename .= $timestamp.'.'.$fileType; $title = mb_convert_encoding($title,'gbk','utf-8'); header('Content-type: application/octet-stream'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Content-Disposition: attachment; filename=' . $filename); echo $title; $ret = $this->download_model->QueryList($Req, $Resp); $content = $this->GetDownLoadContentNew($ret['bill_list'], $fileType); echo $content;
其中GetDownLoadContentNew是对内容进行相应的格式化。
不知道哪里有问题或者有什么地方没注意到?希望大家帮忙看下,谢谢。(fileType是csv或txt,在不同浏览器上都是一样都无法触发下载)
/** * 强制下载文件 * @param string $file 文件路径 */ function force_download($file){ if ((isset($file)) && (file_exists($file))) { header("Content-length: ".filesize($file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment;filename="'.basename($file).'"'); readfile($file); } else { echo "No file selected"; } } //使用示例 force_download('./test.jpg');
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号