Laravel 下载响应不强制下载文件

时间:2021-04-27 15:43:01

标签: php laravel axios laravel-8 content-disposition

这个问题与:Laravel - Force download with response()->download($fileLocation) not working

问题是,我使用 axios GET 请求(如该线程中所述)来启动浏览器下载,但它不起作用。我的控制器工作完美,它正确返回文件,正如我在浏览器控制台中看到的:

请求:

enter image description here

回应

enter image description here

图片

enter image description here

但是浏览器没有开始任何下载。

我在客户端使用 Vue/Nuxt,axios 请求是这样调用的:

  download ({
    dispatch,
    commit,
    rootState,
    rootGetters
  }) {
    this.$axios.get('api/download',
      {
        params: {
          filenames: rootGetters['files/getDownloadFilenames'],
          mode: rootState.config.mode
        }
      }).then((response) => {
      console.log(response)
    }).catch((error) => {
      console.log(error)
    })
  }

使用“getFileDownload”函数调用控制器@laravel:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class DownloadController extends Controller
{
    public function getFileDownload(Request $request) {
        $filenames = $request->input('filenames');
        $mode = $request->input('mode');
        $filePaths = [];
        foreach($filenames as $filename) {
            if ($mode == 'clean') {
                array_push($filePaths, $request->user()->getName() . config('filesystems.transfer_out_clean') . '/' . $filename);
                return response()->download($filePaths[0]);
            }
        }
    }
}

我在这里使用数组的原因是稍后我想返回一个包含多个文件的 ZIP 文件,但正如您所看到的,我只是使用第一个路径返回单个文件以进行测试。

所以我的问题是:为什么即使我使用 GET 请求,这个请求也不开始在客户端上下载该图像文件?

非常感谢任何帮助!

0 个答案:

没有答案
相关问题