2014-01-29 68 views
1

我开始基于phpqrcode的CakePHP Helper。我的问题是,我无法获得生成的PNG或SVG文件,并强制浏览器下载它。CakePHP:下载生成的QR码

我想当一个人通过Ajax提交他的文本时,我为他生成一个QR码并强制浏览器下载它而不保存文件到服务器上。

这里是助手的短例子:

App::import('Vendor', 'QRGenerator.phpqrcode'.DS.'qrlib'); 

class QRHelper extends AppHelper{ 

    function text($content= '') { 

     QRcode::png($content); 

    } 
} 

在我看来文件:

<?php $this->QR->text('example text'); ?> 

而且我的布局:

<?php echo $this->fetch('content'); ?> 

感谢。

+0

尝试以回报或回声'QRCODE :: PNG($内容);''在文本()'。 –

回答

1

在你的控制器试试这个:

$this->response->type('Content-Type: image/png'); 
    $this->response->download('qrcode.png'); 
+0

没有.png物理文件,帮助者直接向浏览器生成.png文件 –

+0

是的,我知道。我发布的代码只是设置响应的标题,以便下载文件,而不是在浏览器中显示。 –

+0

感谢它工作的文件,但对于svg或eps文件,它生成0字节的文件! $ this-> response-> type('eps'); $ this-> response-> download('qrcode.eps'); 这是真的吗? –

1

尝试使用返回或回声QRcode :: png($ content);在文本()

function text($content= '') { 

     return QRcode::png($content); 

    } 
+0

我已经试过,但我没有工作 –

0

尝试用QR Code Helper

  • 复制 “QrCodeHelper.php” 你的“应用程序/查看/助手“文件夹。

  • 在控制器中添加'QrCode'到你的helpers数组中。

  • 在视图中这么做:

    <? echo $this->QrCode->text('Hello World'); ?>