最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - laravel pdf file download from ajax request (laravel 5) - Stack Overflow

programmeradmin1浏览0评论

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:

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

Thank you

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:

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

Thank you

Share Improve this question edited Feb 11, 2019 at 9:11 Jignesh Joisar 15.2k6 gold badges67 silver badges60 bronze badges asked Dec 29, 2015 at 21:25 moses tohmoses toh 13.2k81 gold badges264 silver badges459 bronze badges 8
  • 2 Possible duplicate of Download a file by jQuery.Ajax – user1669496 Commented Dec 29, 2015 at 21:31
  • @user3158900, I think it is not the same. My problem in ajax response. success: function(response) { $('#test').html(response); } – moses toh Commented Dec 29, 2015 at 21:44
  • @user3158900, The response seems wrong. – moses toh Commented Dec 29, 2015 at 21:49
  • Your controller is attempting to return a file download. You are taking that response and attempting to set it as html on the page. It's very hard to tell what you actually want. You want the user to be able to download the pdf file to their machine or do you want the contents of the pdf itself to appear somewhere on the page? – user1669496 Commented Dec 29, 2015 at 21:52
  • 1 @user3158900, Thank you for answering my question. I want the user to be able to download the pdf file to their machine. – moses toh Commented Dec 29, 2015 at 22:14
 |  Show 3 more ments

3 Answers 3

Reset to default 1

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 uping 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
                }
            }

        });

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

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

It's return response, not download file!

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>