2017-09-13 45 views
0

我试图在用户点击路由时下载文件,并且我能够这样做。但是,当用户想要下载docx或doc文件时,文件扩展名不包含.doc。为了更好地问我的问题,我的代码如下:如何指定下载文件的扩展名Laravel 5.0

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做这项工作吗?

回答

1

您可以使用Content-Disposition标题指定下载文件的名称,但不需要手动创建下载响应。 Laravel已经有一个内置INT函数生成与适当的Content-Disposition头下载响应

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

你能告诉我如何从我的代码获得$ fileContent请? – Muhammad

+0

file_get_contents($ path) –

相关问题