Laravel 5:下载文件不显示用于保存的弹出窗口



在我的laravel 5.3中,我将文件上传到" uploads/files/file.pdf"。当我尝试下载它时,使用" file_exists"函数找到文件,但是没有弹出窗口将文件保存到本地计算机中。注意:我通过将文件移动到称为" local"的子目录。

请建议什么问题。

namespace AppHttpControllers;
use Auth;
use IlluminateHttpRequest;
use AppHttpRequests;
use Response;
...............................
public function getDownload()
{
    $file= "uploads/files/file.pdf";
    echo $file;
    if(file_exists($file)){
        dd('File is exists.');
    }else{
        dd('File is not exists.');
    }        
    $headers = array('Content-Type: application/pdf',);
    return Response::download($file, 'filename.pdf', $headers);
}

我认为您还需要一个标题来告诉浏览器打开文件或下载。

因此,在浏览器中下载它

header('Content-Disposition: attachment; filename="filename.pdf"');

在浏览器中打开它

header("Content-Disposition: inline; filename=filename.pdf");

在您的示例中,您应该具有$header变量

$headers = array('Content-Type: application/pdf','Content-Disposition: inline; filename=filename.pdf');

$headers = array('Content-Type: application/pdf','Content-Disposition: attachment; filename="filename.pdf"');

我个人使用它像以下

header("Content-type: application/pdf");
header("Content-Length:" . strlen($file));
//header("Content-Disposition: inline; filename=file.pdf");
header('Content-Disposition: attachment; filename="file.pdf"');
print $file;

,或者只是在您的情况下像以下

那样做
return response()->download("uploads/files/file.pdf");

整个控制器代码在

以下
namespace AppHttpControllers;
use Auth;
use IlluminateHttpRequest;
use AppHttpRequests;
use Response;
...............................
public function getDownload()
{
    $file= "uploads/files/file.pdf";
    //echo $file;
    if(file_exists($file)){
        //dd('File is exists.');
        return response()->download($file);
    }else{
        //dd('File is not exists.');
        abort(404);
    }  
}

最新更新