CakePHP:下载生成的QR码
问题描述:
我开始使用基于 phpqrcode 的CakePHP助手。我的问题是,我不能得到生成的png或svg文件,并强制浏览器下载它。
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.
我想要当一个人通过Ajax提交他的文本,我生成一个QR码为他和强制浏览器下载它,而不保存在服务器上的文件。
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'); ?>
我的布局:
<?php echo $this->fetch('content'); ?>
谢谢。
答
在您的控制器中尝试此操作:
Try this in your controller:
$this->response->type('Content-Type: image/png');
$this->response->download('qrcode.png');