I need to add a button in my view file which allows a user to download a file from the front end of the website. Everything is working fine re: displaying data however I'm not sure how to offer up a downloadable file based on data in a variable.
Here is the data which is being generated from the view file.
<?php
$vCardData[$i] = '
BEGIN:VCARD
VERSION:4.0
N:'.${'data'.$i}['l_name'].';'.${'data'.$i}['f_name'].';;;
FN:'.${'data'.$i}['title'].' '.${'data'.$i}['f_name'].' '.${'data'.$i}['l_name'].'
ORG:'.${'data'.$i}['company'].'
TEL;TYPE=work,voice;VALUE=uri:tel:'.${'data'.$i}['company_contact_number'].'
TEL;TYPE=home,voice;VALUE=uri:tel:'.${'data'.$i}['number'].'
ADR;TYPE=work;LABEL="'.${'data'.$i}['address_1'].' '.${'data'.$i}['address_2'].'\n'.${'data'.$i}['city'].', '.${'data'.$i}['county'].' '.${'data'.$i}['post_code'].'\n'.${'data'.$i}['county'].'"
:;;'.${'data'.$i}['address_1'].' '.${'data'.$i}['address_2'].';'.${'data'.$i}['city'].';'.${'data'.$i}['county'].';'.${'data'.$i}['post_code'].';'.${'data'.$i}['county'].'
EMAIL:'.${'data'.$i}['email'].'
REV:'.time().'
END:VCARD
';
?>
I need to somehow use the CodeIgniter force_download()
on click of an anchor from within the view file. I have tried adding <?php echo site_url( 'controller/function' ); ?>
<?php echo site_url( 'controller/function' ); ?>
to the anchor tag however this is just directing me to a page on the site http://example.com/controller/function and I'm assuming that's because it's called from the front end.
Is there any way to achieve this?
Further notes: My controller is selecting all the data from the database and passing it to my address_book view. In the address_book view I have the following:
<?php for ($i = 0; $i < $count; $i++) { ?>
<div class="col-sm-6 contact_card">
<h4><?php echo ${'data'.$i}['title']; echo ${'data'.$i}['f_name']; echo ${'data'.$i}['l_name']; ?></h4>
<?php if (${'data'.$i}['email']) { echo 'e: <a href="mailto:'.${'data'.$i}['email'].'">'.${'data'.$i}['email'].'</a>'; }
if (${'data'.$i}['number']) { echo '<p>t: '.${'data'.$i}['number'].'</p>'; }
$vCardData[$i] = '
BEGIN:VCARD
VERSION:4.0
N:'.${'data'.$i}['l_name'].';'.${'data'.$i}['f_name'].';;;
FN:'.${'data'.$i}['title'].' '.${'data'.$i}['f_name'].' '.${'data'.$i}['l_name'].'
ORG:'.${'data'.$i}['company'].'
TEL;TYPE=work,voice;VALUE=uri:tel:'.${'data'.$i}['company_contact_number'].'
TEL;TYPE=home,voice;VALUE=uri:tel:'.${'data'.$i}['number'].'
ADR;TYPE=work;LABEL="'.${'data'.$i}['address_1'].' '.${'data'.$i}['address_2'].'\n'.${'data'.$i}['city'].', '.${'data'.$i}['county'].' '.${'data'.$i}['post_code'].'\n'.${'data'.$i}['county'].'"
:;;'.${'data'.$i}['address_1'].' '.${'data'.$i}['address_2'].';'.${'data'.$i}['city'].';'.${'data'.$i}['county'].';'.${'data'.$i}['post_code'].';'.${'data'.$i}['county'].'
EMAIL:'.${'data'.$i}['email'].'
REV:'.time().'
END:VCARD
';?>
<a href="<?php echo site_url('address_book/vcard') ?>">Download vCard</a>
</div>
<?php }
Have you considered handling this at the server level rather than in the application? Your webserver could/should be configured to return .vcf as attachments. Related: stackoverflow.com/questions/5116772/…
Perhaps EE overwrites those headers tho? If so here's a free EE extension that allows you to extend template types (each of which can have their own mime type):
http://devot-ee.com/add-ons/template-image-mime-type
You could easily modify this extension to include the download handling you require.
I think you want: application/octet-stream
for .vcf
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.