Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel pdf file download from ajax request (laravel 5)

My html 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)
        {
            var file_path = file_path.replace(/\\/g,"/");
            //example : file_path = assets/images/myfile.pdf
            $.ajax({
                type: "POST",
                data: 'file_path=' + file_path,
                url: "news/test",
                success: function(response)
                {
                    $('#test').html(response);

                }
            });
        }

My function test in controller :

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

When I click on the pdf icon, no response.

I wish, when click on the pdf icon, appear like this:

enter image description here

how to keep when click pdf icon, the image appears like it?

Thank you

like image 637
moses toh Avatar asked Dec 29 '15 21:12

moses toh


1 Answers

What I have done is, written two separate route one to verify and one to download. On success of one ajax I have triggered window.open(downloadUrl,'_blank') to download in separete window. It is not the way asked but it prevents any upcoming errors as verify url will sort that

$.ajax({
            url: verifyUrl,  
            type: 'get',
            cache: false,
            data: null,
            error: function (err){$('#ajax_loader_div').hide();},
            success: function(response) {
                console.log(response);
                $('#ajax_loader_div').hide();
                if (response.status == 'success') 
                {

                    window.open(downloadUrl,'_blank');

                }else if(response.status == 'error') 
                {
                    //show error
                }
            }

        });
like image 62
Surya Pratim Mukherjee Avatar answered Sep 25 '22 14:09

Surya Pratim Mukherjee