返回

thinkphp download()函数下载中文名文件名乱码

在使用fetch请求文件下载接口时候,发现响应头的content-disposition中的filename总是报错;后来才知道header中只支持ASCII,所以使用thinkphp的download()函数时候需要进行以下修改:

//vendor\topthink\framework\src\think\response\File.php::output()

        //增加这两行
        $encodedName = rawurlencode($name);
        $disposition = "filename=\"{$encodedName}\"; filename*=UTF-8''{$encodedName}";

        $this->header['Pragma']                    = 'public';
        $this->header['Content-Type']              = $mimeType ?: 'application/octet-stream';
        $this->header['Cache-control']             = 'max-age=' . $this->expire;
        $this->header['Content-Disposition']       = ($this->force ? 'attachment; ' : '') . $disposition; //'filename="' . $name . '"';
        $this->header['Content-Length']            = $size;
        $this->header['Content-Transfer-Encoding'] = 'binary';
        $this->header['Expires']                   = gmdate("D, d M Y H:i:s", time() + $this->expire) . ' GMT';

评论