【问题标题】:Redirect view after download pdf下载pdf后重定向查看
【发布时间】:2019-07-05 04:03:50
【问题描述】:

我有一个视图viewarticles。在那里,存在一个锚标签,用于下载 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 文件然后返回同一页面。

【问题讨论】:

  • 你没有被重定向的原因是因为你每个块只能return一次,return Response::download();之后的任何代码都不会被执行。也许this 问题可以帮助解决这个问题。
  • 尝试其他方式: 1. 创建文件。 2. 将下载链接保存到会话/cookie 或您喜欢的任何内容。 3. 重定向页面。 4. 自动调用该下载链接。
  • 如何在会话中保存下载链接?

标签: php jquery ajax laravel-5 eloquent


【解决方案1】:

改变

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

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

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

【讨论】:

  • 您正试图将用户引导至两个不同的 URL,一个属于 PDF,另一个属于另一个页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多