헤더 구현 파일 다운로드 문제

WBOY
풀어 주다: 2023-03-01 20:38:02
원래의
1275명이 탐색했습니다.

버튼을 눌러야 정보를 얻을 수 있고, 콘텐츠를 파일 형태로 다운로드할 수 있습니다.
이제 버튼을 클릭하면 다운로드 창이 팝업되는 대신 새 창이 열리고 콘텐츠가 표시됩니다.
코드는 다음과 같습니다.

<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는 그에 따라 콘텐츠 형식을 지정합니다.
무슨 문제가 있는지 모르거나 아무것도 눈치채지 못하셨나요? 살펴보는 데 도움이 되었으면 좋겠습니다. 감사합니다. (파일 형식은 csv 또는 txt이며 다른 브라우저에서도 동일하지만 다운로드를 실행할 수 없습니다.)

답글 내용:

버튼을 눌러야 정보를 얻을 수 있고, 콘텐츠를 파일 형태로 다운로드할 수 있습니다.
이제 버튼을 클릭하면 다운로드 창이 팝업되는 대신 새 창이 열리고 콘텐츠가 표시됩니다.
코드는 다음과 같습니다.

<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는 그에 따라 콘텐츠 형식을 지정합니다.
무슨 문제가 있는지 모르거나 아무것도 눈치채지 못하셨나요? 살펴보는 데 도움이 되었으면 좋겠습니다. 감사합니다. (파일 형식은 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>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!