亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在laravel中下載上傳的文件

如何在laravel中下載上傳的文件

PHP
慕娘9325324 2022-01-14 16:54:17
我想創建一個函數來將上傳的文件下載到存儲中,但它總是顯示錯誤代碼:文件“D:\xampp php 7.2.2\htdocs\siapKerja_laravel\public\files/22”不存在public function show($idFile) {     $file = public_path(). "/files/";    return Response()->download($file.$idFile);$ } Route::get('/verifikasi/pkwt/download/{id}', 'FileController@show')->name('downloadFile');
查看完整描述

2 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

我希望它對你有幫助。我在“文件”文件夾中上傳了文件,而不是在存儲文件夾中。您只需根據 that.set 您的 content_types 根據您上傳的文件進行更改。


我的控制器代碼是


public function  openPdf(Request $request,$docFile)

{   


    $file='/var/www/html/laravelQuiz/public/files/'.$docFile;

     $extension = explode(".",$docFile);


     foreach($extension as $extensions){

        if($extensions == 'pdf'){

           $content_types =  'application/pdf';

        }elseif($extensions == 'doc'){

           $content_types =  'application/msword';

        }elseif($extensions == 'txt'){

            $content_types =   'application/octet-stream';

        }elseif($extensions == 'docx'){

            $content_types =     'application/vnd.openxmlformats-officedocument.wordprocessingml.document';

        }elseif($extensions == 'ppt'){

            $content_types = 'application/vnd.ms-powerpoint';

        }elseif($extensions == 'odt'){

            $content_types = 'application/vnd.ms-powerpoint';

        }elseif($extensions == 'txt'){

            $content_types = 'text/plain';

        }

     }

     $content = file_get_contents($file);               

     return Response::make($content, 200, array('content-type'=>$content_types));


}


查看完整回答
反對 回復 2022-01-14
?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

您需要提供文件的完整 url。

嘗試添加文件擴展名:

$extension = ".png" // or whatever type file is

return Response()->download($file.$idFile.$extension);

保存文件時,您還可以在存儲文件時獲取擴展名:

$extension = $request->file('file')->extension();


查看完整回答
反對 回復 2022-01-14
  • 2 回答
  • 0 關注
  • 228 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號