weixin_33739523 2016-03-30 09:42 采纳率: 0%
浏览 33

PHP响应字符串作为文件

in php create pdf as string, something like this:

%PDF-1.4
(..skipping...)
/F1 9 Tf
0 g 
BT
157 830 Td
(Lorem ) Tj
ET
(..skipping...)
8 0 obj 
<</Type /Font /Encoding /WinAnsiEncoding /Subtype /Type1 /BaseFont /Helvetica >>
endobj

My php code:

//string it's pdf as plain text
return new Response($string);

In js I use ajax:

$("a#get_pdf").on('click', function(e){
    e.preventDefault();
    $.ajax('/get_pdf/');
});

How I can download this file, because I have response as plain text, don't like a file.

-----EDIT-----

I can do this, like in this answer: https://stackoverflow.com/a/34065784/4900669

-----EDIT 2----

But after some search I edit my js code(https://stackoverflow.com/a/20830337/4900669):

$("a#get_pdf").on('click', function(e){
    e.preventDefault();
    $.ajax({
        url: '/get_pdf/',
        type: 'POST',
        success: function() {
            window.location = '/get_pdf/';
        }
    });
});

And it's work perfectly.

  • 写回答

2条回答 默认 最新

  • weixin_33691817 2016-03-30 09:44
    关注

    First of all I don't think you need to make this as ajax, just let the browser open the link in a blank window.

    Then you need to fix your headers to make the download possible, reference: Symfony2 - Force file download

    This should be enough to make it work! I hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料