Maison  >  Article  >  développement back-end  >  header实现文件下载的问题

header实现文件下载的问题

WBOY
WBOYoriginal
2016-12-01 00:56:191271parcourir

需要实现点击button然后获取信息,将内容以文件形式下载下来。
但是现在点击button是打开新窗口并且展示出内容,而不是弹出下载窗。
代码如下:

<code>$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;
</code>

其中GetDownLoadContentNew是对内容进行相应的格式化。
不知道哪里有问题或者有什么地方没注意到?希望大家帮忙看下,谢谢。(fileType是csv或txt,在不同浏览器上都是一样都无法触发下载)

回复内容:

需要实现点击button然后获取信息,将内容以文件形式下载下来。
但是现在点击button是打开新窗口并且展示出内容,而不是弹出下载窗。
代码如下:

<code>$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;
</code>

其中GetDownLoadContentNew是对内容进行相应的格式化。
不知道哪里有问题或者有什么地方没注意到?希望大家帮忙看下,谢谢。(fileType是csv或txt,在不同浏览器上都是一样都无法触发下载)

<code>/**
  * 强制下载文件 
  * @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');</code>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn