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.

查看全部
weixin_33739523
weixin_33739523
2016/03/30 09:42
  • response
  • php
  • symfony
  • ajax
  • 点赞
  • 收藏
  • 回答
    私信

2个回复