下载pdf后重定向视图

时间:2019-02-11 15:53:14

标签: php jquery ajax laravel-5 eloquent

我有一个观点。因此,存在一个锚标记,可用于下载PDF。当我第一次单击链接时,注册表单将以模式形式打开,如果用户提交了他的详细信息,则它将进入控制器并保存详细信息,然后创建会话,然后下载PDF。但是我也需要返回相同的观点。创建重定向会话后,无需再次填写详细信息。意思是一次填写表格并每次下载PDF直到会话有效。

@foreach($articles as $article)
 <tr class="table-active">
@if(session()->has('username'))
  <td>
 <a href="{{ route('downloadpdf',['jname' => $article->image]) }}"><span class="badge badge-primary">Download PDF</span></a>
@else
  <a href="#" data-toggle="modal" data-target="#login-modal{{ $article->id }}">Login</a>
@endif
 </td></tr>
<!-- Register modal -->
<div class="modal fade" id="login-modal{{ $article->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
  <div class="loginmodal-container">
     <h1>Article Download Request</h1><br>
     <form method="POST" action="{{ route('insertdownloadpdf') }}">
      {{csrf_field()}}
       <input type="text" name="name" placeholder="Name">
      <input type="text" name="pdfname" placeholder="Name" value="{{ $article->image }}">
       <input type="text" name="volume_id" placeholder="Name" value="{{ $article->volume_id }}">
     <input type="email" name="email" placeholder="Email">
       <input type="text" name="designation" placeholder="Designation">
       <input type="text" name="organisation" placeholder="Organisation/College">
      <input type="number" name="number" placeholder="Phone Number">
      <input type="submit" class="login loginmodal-submit" value="Submit">
       </form>
  </div>
   </div>
 </div>
 @endforeach

我的路线是:

Route::POST('insertdownloadpdf','foruserview@insertdownloadpdf')->name('insertdownloadpdf');

控制器方法是:

public function insertdownloadpdf(Request $request)
{
    $userdetails = new userdetail;

   $userdetails->name = $request->name;
   $userdetails->email = $request->email;
   $userdetails->designation = $request->designation;
   $userdetails->organisation = $request->organisation;
   $userdetails->number = $request->number;
   $userdetails->save();
   $pdffile = $request->pdfname;
   session()->put('username','Successfully Inserted');

 $file= public_path(). "/storage/upload_pic/";  //path of your directory
            $headers = array(
                'Content-Type: application/pdf',
            );
    return Response::download($file.$pdffile, 'articles.pdf', $headers);

     $journals = journal::all();
    $articles = article::where('volume_id',$request->volume_id)->with('journal','volume')->get();

   return view('viewarticles',compact('journals','articles'));
}

但是它不起作用。如何下载PDF文件,然后返回同一页面。

1 个答案:

答案 0 :(得分:0)

更改

return Response::download($file.$pdffile, 'articles.pdf', $headers);

Response::download($file.$pdffile, 'articles.pdf', $headers);

在一个方法内只能返回一次。