【问题标题】:How to download .docx documents with Vue/Laravel如何使用 Vue/Laravel 下载 .docx 文档
【发布时间】:2021-02-08 12:13:41
【问题描述】:

我使用带有 Laravel API 的 Vue。我创建了一个文件管理器。 PDF 文件可以很好地下载。但是 .docx 文件在下载后已损坏。

控制器

    public function downloadFile(Request $request, File $file)
    {
        $headers = array(
            'Content-Type: '.$file->mime_type,
        );
        return Storage::disk('local')->download($file->path, $file->name, $headers);
    }

Vue/Vuex

export const downloadFile= ({ commit }, {endpoint, file}) => {
    return axios.get(endpoint).then((response) => {
        console.log(file.mime_type )
        let blob = new Blob([response.data], { type: file.mime_type })
        let link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = file.name
        link.click()
    })
}

当我从服务器下载 PHP Storm 中的文件时,文件没问题。因此,上传过程运行良好。

但我无法打开下载的文件。

我发现了一些关于 ob_clean 的东西,但不明白解决方案。

【问题讨论】:

  • 你试过返回return Response::download($file, 'file_name.ext', $headers);吗?也可以在标题中添加内容长度

标签: laravel vue.js


【解决方案1】:

我试过了,但对我也不起作用 不要使用 ajax 或 axios 使用 get-request 发送您的请求,并在您的函数中返回下载响应:

return response()->download(public_path('uploads/TestWordFile.docx'));

您不需要发送 ajax 请求。 使用 GET 并且当您返回一些响应时,页面不会被刷新! 这应该适合你

【讨论】:

    【解决方案2】:

    尝试使用 ....

    public function downloadFile(Request $request, File $file)
    {
        return Storage::disk('local')->download($file);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 2020-01-27
      • 1970-01-01
      • 2018-11-17
      • 2022-01-26
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多