[英]How to download a file from the view on button click?
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. 我需要以某种方式在视图文件中单击锚点时使用CodeIgniter
force_download()
。 I have tried adding <?php echo site_url( 'controller/function' ); ?>
我试过添加
<?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. <?php echo site_url( 'controller/function' ); ?>
锚标记,但这只是将我定向到网站http://example.com/controller/function上的页面,我认为那是因为它是从前端调用的。
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. 进一步说明:我的控制器正在从数据库中选择所有数据,并将其传递到我的address_book视图。 In the address_book view I have the following:
在address_book视图中,我具有以下内容:
<?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.
您的网络服务器可以/应该配置为返回.vcf作为附件。 Related: stackoverflow.com/questions/5116772/…
相关:stackoverflow.com/questions/5116772/…
Perhaps EE overwrites those headers tho? 也许EE会覆盖这些标头吗? If so here's a free EE extension that allows you to extend template types (each of which can have their own mime type):
如果是这样,那么这里有一个免费的EE扩展,可让您扩展模板类型(每种模板都可以具有自己的mime类型):
http://devot-ee.com/add-ons/template-image-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 我认为您想要:.vcf的
application/octet-stream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.