繁体   English   中英

如何为下载的文件Laravel 5.0指定扩展名

[英]How to specify the extension for a downloaded file Laravel 5.0

当用户点击路线时,我正在尝试下载文件,而我能够这样做。 但是,当用户要下载docx或doc文件时,文件扩展名中没有.doc。 为了更好地问我的问题,我的代码如下:

Route::get('resumeDownload', function() {
    $user = Auth::user();
    $path = $user->cv()->first()->path; //when the user uploads a document, i store
    //the path of it in the database
    $extension = explode(".", $path)[1]; //if the user uploaded a doc, returns .doc etc
    $uploaded = Storage::get($path); //I get it from my storage
    return (new \Illuminate\Http\Response($uploaded, 200))
           ->header('Content-Type', 'application/pdf');
});

当用户想要下载pdf时,此代码非常有用,但是对于.doc文件或.docx文件,下载的文件没有扩展名。 我还保存了扩展名,并且可以轻松地对其进行检索(如果有帮助的话)。 无论如何,.doc,.docx,.pdf和.txt可以使用此功能吗?

您可以使用Content-Disposition标头指定下载文件的名称,但是无需手动创建下载响应。 Laravel已经具有内置的int函数,可以使用适当的Content-Disposition标头生成下载响应

$headers = ['Content-Type' => 'application/pdf'];
return response()->download($fileContent, 'filename.pdf', $headers);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM