来自ajax请求的Laravel React Force浏览器下载文件?

时间:2017-06-09 16:49:00

标签: javascript laravel reactjs laravel-5 laravel-4

我使用Laravel和React js。我想下载文件,所以我使用return response()->download($pathToFile)其中pathToFile位于公共dir.my url由auth中间件保护。 我在Force Download via Ajax and PHP尝试了解决方案,但没有工作。

另一方面,我使用axios发送ajax请求来获取文件,我得到像response header

这样的响应头

并获得像enter image description here

这样的编码响应

我如何强制浏览器下载文件?

1 个答案:

答案 0 :(得分:2)

我正在使用axios,所以我添加了responseType:'blob'来请求标题然后使用filesave或responseType:'blob'或react-file-download来下载文件

axios.get(url, {responseType: 'blob'})
    .then((res) => {
        FileDownload(res.data, filename);
    });
相关问题