download方法在safari中文件名为中文是乱码

cbasil

下载图片调用downlaod方法在safari中中文名是乱码,代码和错误截图如下,
通过设置请求头Content-type:text/html;charset=utf-8能解决不
return response()->download(base_path() . '/resource/' . $id . '.jpg', 'avatar.jpg');
截图

3038 1 0
1个回答

zhaohanfeng

不要直接用downlaod方法,构造一下header信息,贴一段我目前的下载

            $sourceName = '某某某.mp4';
            $encoded_filename = urlencode($sourceName);
            $encoded_filename = str_replace("+", "%20", $encoded_filename);

            $encodefilename = rawurlencode($sourceName);
            $user_agent = strtolower($request->header('user-agent'));
            $filepath = '/data/xxx.mp4';

            if (file_exists($filepath)) {
                $respone = response();
                $respone->withFile($filepath);
                if (preg_match("/msie|edge/", $user_agent)) {
                    $respone->header('Content-Disposition', "attachment; filename=\"$encoded_filename\"");
                } elseif (preg_match("/safari/", $user_agent)) {
                    $respone->header('Content-Disposition', "attachment; filename=\"$sourceName\"");
                } else {
                    $respone->header('Content-Disposition', "attachment; filename=\"$sourceName\"; filename*=utf-8\"$encodefilename\"");
                }
                return $respone;
            } else {
                throw new Exception('文件不存在,下载文件失败');
            }
年代过于久远,无法发表回答
×
🔝