Laravel-强制下载响应()->下载($fileLocation)不起作用



我正在使用Laravel创建下载功能。当用户单击"下载"按钮时,我发起对控制器的 ajax 调用,如下所示 -

public function download(Resource $resource) {
// Force download of the file
$file_to_download   = 'https://data.domain.com/downloads/' . $resource->file_name;
$temp_file_location =   public_path('/tmp_files/' . $resource->file_name);
copy($file_to_download, $temp_file_location);
return response()->download($temp_file_location)->deleteFileAfterSend(true);
}

Chrome 的检查器显示响应中填充了文件的内容,但不会触发实际下载。

我一直在努力寻找答案,但到目前为止还没有成功。非常感谢您的帮助。

谢谢你的时间。

您无法从 Ajax 请求下载文件,因为出于安全考虑,Javascript 无法将文件保存到文件系统。

有一些很好的软件包,如Jquery文件下载

或者您可以使用传统的 GET 请求。

最新更新