Home  >  Article  >  Backend Development  >  The problem of header implementation file download

The problem of header implementation file download

WBOY
WBOYOriginal
2016-12-01 00:56:191320browse

You need to click the button to get the information and download the content in the form of a file.
But now clicking the button opens a new window and displays the content instead of popping up a download window.
The code is as follows:

<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 formats the content accordingly.
Don’t know what’s wrong or haven’t noticed something? I hope you can help me take a look, thank you. (The fileType is csv or txt, which is the same on different browsers but cannot trigger the download)

Reply content:

You need to click the button to get the information and download the content as a file.
But now clicking the button opens a new window and displays the content instead of popping up a download window.
The code is as follows:

<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 formats the content accordingly.
Don’t know what’s wrong or haven’t noticed something? I hope you can help me take a look, thank you. (The fileType is csv or txt, which is the same on different browsers but cannot trigger the download)

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn