CakePHP: download generated QR Code

可紊 提交于 2019-12-11 14:13:22

问题


I'm starting a CakePHP Helper based on phpqrcode. My problem is that i can't get the generated png or svg file and force the browser to download it.

i want to when a person submit his text via Ajax, i generate a QR Code for him and force the browser to download it without saving the file on the server.

Here is a short example of the Helper:

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

class QRHelper extends AppHelper{

    function text($content= '') {

        QRcode::png($content);

    }   
}

In my view file:

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

And my layout:

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

Thanks.


回答1:


Try this in your controller:

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



回答2:


Try with return or echo QRcode::png($content); in text()

  function text($content= '') {

       return QRcode::png($content);

    }



回答3:


Try with QR Code Helper:

  • Copy the "QrCodeHelper.php" to your "app/View/Helper" folder.

  • In the Controller add 'QrCode' to your helpers array.

  • In the view do for example:

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


来源:https://stackoverflow.com/questions/21430837/cakephp-download-generated-qr-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!