0

My code is like this :

<a href="javascript:;" onclick="showAjaxPdf('{{ $row->file_path }}');"><i class="fa fa-file-pdf-o"></i></a>

My javascript code is like this :

  function showAjaxPdf(file_path){
//example : file_path = assets/images/myfile.pdf
                $.post("news/test", { name: file_path } ); 
            }

My function test in controller :

public function postTest($file_path)
    {  
        return response()->download($file_path);       
    }

When I click on the pdf icon, failed to send parameter from javascript to controller

I wish, when click on the pdf icon, appear like this: https://i.stack.imgur.com/s1VFw.jpg

Thank you

moses toh
  • 12,344
  • 71
  • 243
  • 443

3 Answers3

0

You are posting parameter name not $file_path

use Illuminate\Http\Request;

public function postTest(Request $request)
{  
    $file_path = $request->input('name');
    return response()->download($file_path);       
}

make sure your $file_path meaningful for your server to resolve path on server public folder public_path() will get you <driver>/<dir>/<app dir>/public/images/smail.png then you can download it

return response()->download(public_path() + $file_path);

0

This is how you retrieve input in Laravel 5.

public function postTest()
{  
    $file_path = Request::input('name');     // get the value that was posted
    return response()->download($file_path); // return something    
}

Also, I am assuming that in your routes.php file, you have "news/test" routing to postTest

https://laravel.com/docs/master/requests#retrieving-input

https://laravel.com/docs/5.0/routing#basic-routing

thedarklord47
  • 3,183
  • 3
  • 26
  • 55
0

have you tried exclamation mark (!!) ?

<a href="javascript:;" onclick="showAjaxPdf('{!! $row->file_path !!}')">
Albert Abdonor
  • 921
  • 8
  • 15