【问题标题】:how to view file pdf in browser without downloading in laravel 5.6如何在浏览器中查看文件 pdf 而无需在 laravel 5.6 中下载
【发布时间】:2018-10-31 18:49:19
【问题描述】:

谁能修复我的代码

这是我的路线

Route::post('/ViewFile/{nama_file}', 'HSEController@getDownloadFile')->name('DownloadFile');

这是我的观点

<a href="{{ route('DownloadFile', $temuan->file) }}" target="_blank">File Lamp</a>

这是我的控制器

public function ViewFile($nama_file)
{

  $file= public_path("/files/".$nama_file);
  $headers = [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; '.$nama_file,
     ];

  return response()->file($file, $headers);

}

我使用了该代码,结果是文件总是在下载,我想更改为在我的浏览器中打开,

【问题讨论】:

  • 试试这个:&lt;a href="{{ route('DownloadFile', $temuan-&gt;file) }}" target="_blank"&gt;File Lamp&lt;/a&gt; 现在它会给你一个在新窗口中下载和打印的选项!!
  • 不,先生,仍然无法工作@HirenGohel
  • 在您的控制器代码中也进行此更改:pastebin.com/QG2UDfTw
  • 也不行@HirenGohel
  • 这对我有用!也请参考这个答案:stackoverflow.com/questions/25938294/…

标签: laravel laravel-5 laravel-5.6


【解决方案1】:

试试这个:

public function ViewFile($nama_file)
{

  $file= public_path("/files/".$nama_file);
  $headers = [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; '.$nama_file,
     ];

  return response()->streamDownload(function () {
    //do something here
     }, $file, $headers);

}

【讨论】:

    【解决方案2】:

    尝试将 Content Disposition 设置为内联。

    return response()->file($file_path, [
      'Content-Disposition' => 'inline; filename="'. $file_path .'"'
    ]);
    

    【讨论】:

    • 使用上述代码,PDF是否还在下载中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多