laravel - 如何使用 Vue + Laravel 强制下载文件
问题描述
我正在尝试使用从 laravel 检索的 vue 强制下载文件(不一定是 PDF!)。
我到处环顾四周,我似乎只能找到当我知道它总是会是 PDF 时的方法。但是,如果可能有几种不同的文件类型怎么办。
解决方案
使用 laravel,你可以编写函数下载。也许
$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
示例文件下载是 docx 文件,然后内容类型是 docx 的 mime =>application/vnd.openxmlformats-officedocument.wordprocessingml.document
使用vue,可以使用axios下载文件。
推荐阅读
- spyder - PyMC3:Spyder 中的 RuntimeError
- amazon-web-services - Amazon AppSync 是否支持 Amazon QLDB?
- javascript - JavaScript 正则表达式包含,但不完全匹配
- java - 如何比较 Java 中的路径等效性?
- xml - xslt to flatten xml hierarchy using attribute value
- javascript - e.target 为空,当我使用 setState 时
- javascript - Electron.js 如何在 html 脚本标签中使用 require 函数
- sql-server - 如何从 sql 脚本逆向工程生成 ER 图
- c++ - C++ 运算符 - 方法
- java - 我无法从 android studio 中的子活动返回数据