从ajax请求下载laravel pdf文件(laravel 5)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34519319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:55:43  来源:igfitidea点击:

laravel pdf file download from ajax request (laravel 5)

javascriptajaxlaravelpdfdownload

提问by moses toh

My html code is like this :

我的 html 代码是这样的:

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

My javascript code is like this :

我的 javascript 代码是这样的:

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.

当我点击pdf图标时,没有反应。

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

我希望,当单击 pdf 图标时,显示如下:

enter image description here

在此处输入图片说明

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

如何保持点击pdf图标时,图像看起来像它?

Thank you

谢谢

回答by Surya Pratim Mukherjee

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 成功时,我触发了 window.open(downloadUrl,'_blank') 在单独的窗口中下载。这不是询问的方式,但它可以防止任何即将发生的错误,因为验证 url 将对其进行排序

$.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
                }
            }

        });

回答by HItesh Tank

for that you need to set the header with response object. Please see the below code.

为此,您需要使用响应对象设置标头。请看下面的代码。

    $headers = array(
                  'Content-Type'=> 'application/pdf'
                );
$file_path = $request->input('file_path');  
 //TODO: you have to split the file name from url 
 return Response::download($file_path, '<filename>', $headers);

I hope this code will help you

我希望这段代码能帮助你

回答by Danillo Le?o Lopes

return Response::download($file_path, '<filename>', $headers);

返回 Response::download($file_path, '<filename>', $headers);

It's return response, not download file!

这是返回响应,而不是下载文件!