且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel-文件下载

更新时间:2023-11-13 23:11:52

尝试一下:

public function getDownload($id)
{
    // $file = File::get("../resources/logs/$id");
    $headers = array(
       'Content-Type: application/octet-stream',
    );
    #return Response::download($file, $id. '.' .$type, $headers); 
    return response()->download("../resources/logs/$id", $id.'txt', $headers);
}

从文档中

下载方法可用于生成强制执行以下操作的响应: 用户浏览器以指定路径下载文件.

The download method may be used to generate a response that forces the user's browser to download the file at the given path.

return response()-> download($ pathToFile,$ name,$ headers);

return response()->download($pathToFile, $name, $headers);

https://laravel.com/docs/5.1/responses#basic-responses