2017-09-13 46 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) –

相關問題