繁体   English   中英

怎么说浏览器开始下载文件?

[英]How to say browser to start download file?

使用客户端,我向Laravel发送POST请求以将文件返回到浏览器:

$headers = [
             'Content-Type' => 'application/vnd.ms-excel',
             'Content-Disposition' => "attachment; filename='Report.xls'"
        ];

        return response()->download(storage_path('app/'.$path.$filename), $filename, $headers);

它返回我二进制文件作为响应:

在此处输入图片说明

响应头是:

在此处输入图片说明

尝试使用:

return Storage::download('file.jpg', $name, $headers);

参考: https : //laravel.com/docs/5.8/filesystem#downloading-files

编辑1:

可能的解决方案:

创建一条路线来获取xls文档(使用GET http方法),按他的名字,例如谁返回:

return Storage::download('file.jpg', $name, $headers);

进行POST请求,返回204 http代码并带有Location标头。

return response()->header('Location', $url)

调用AJAX成功事件时,请执行以下操作:

success: function(data, textStatus, request) {
    window.open(request.getResponseHeader('Location'), '_blank');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM