Monday, August 19, 2013

CakePHP 2.3 How to set header for download data?

I've been using PHP for a while, but am new to Cake. What I am attempting to do is create a vCard on the fly in a PHP variable and download it to a client.  To do that, I need to set the response header to something like "Content-Type: text/x-vcard; charset=utf-8" and then add a 'Content-Disposition: attachment ; filename=\"vcardname.vcf\" ' header. I have done this successfully in standalone PHP by just using the "header" function, but it seems that no matter what I do, the CakePHP view just displays the vCard in text and sets the content type to text/html.

I've been Googling and reading for a couple of days and have not made any progress. I freely admit that I'm probably missing something basic, but I just can't figure out what. 

My download.ctp view file looks like this:

....Some code to make a vcard from a DB record in $vcard

header("Content-Type: text/x-vcard; charset=utf-8");
header("Content-Disposition: attachment; filename=\"vcardname.vcf\";");
echo $vcard;

Pretty basic.  If I run this code as a standalone PHP script, it downloads the file to my browser as expected. When I run it as a Cake view tied to a controller action, it just echoes the vcard variable to the screen and leaves the content type as text/html.

Any advice would be greatly appreciated!

Thanks,

Gray

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: