0

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.

Vinod
  • 123
  • 1
  • 3
  • 14
  • 1
    The reason you're not redirected is because you can only `return` once per block, any code after `return Response::download();` won't be executed. Maybe [this](https://stackoverflow.com/questions/25624927/how-do-i-redirect-after-download-in-laravel) question can be of help how to get around this. – Alex Feb 11 '19 at 15:59
  • Try other way: 1. Create file. 2. Save download link to session/cookie or whatever you like. 3. Redirect page. 4. Call that download link automaticaly. – bakis Feb 11 '19 at 16:01
  • How to save download link in session? – Vinod Feb 11 '19 at 16:05

1 Answers1

0

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.

edward
  • 142
  • 1
  • 6
  • You are trying to direct the user to two distinct URLs, one belonging to the PDF and one of another page. – edward Feb 11 '19 at 16:16