dounuogong0358 2019-02-11 15:53
浏览 72

下载pdf后重定向视图

I have a view viewarticles. In that, an anchor tag is present, which is for download PDF. When I click first time on link, then a register form opens in modal and if user submits his details, then it goes on controller and save the details and then create session and then download the PDF. But I also I need return same view. After redirect session created and no need to fill details again. Means one time fill the form and download PDF each time till session is valid.

@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

My route is:

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

Controller method is:

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'));
}

But it is not working. How can I download PDF file then return on same page.

  • 写回答

1条回答 默认 最新

  • duanlujiaji10335 2019-02-11 16:02
    关注

    Change

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

    to

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

    You can only return once within a method.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭