从视图调用控制器方法

Calling a controller method from view

本文关键字:方法 控制器 调用 视图      更新时间:2023-09-26

我的控制器中有一个方法

public function download($filepath){
$download_path = $_SERVER['DOCUMENT_ROOT']. "mediabox/import";
$file = $download_path + $filepath ;
if(!$file) die("I'm sorry, you must specify a file name to download.");
if(eregi("'.ht.+", $file)) die("I'm sorry, you may not download that file.");
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
    header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header('Pragma: no-cache');
header('Expires: 0');
    // Send the file contents.
readfile($file);
}

在我看来,我有一个链接

<a target='_blank' class='download_dialog' onClick="???">

我想调用我的控制器的下载方法onclick事件的链接。这样做好吗?我该怎么做?感谢

您可以尝试:

<a target='_blank' class='download_dialog' href="<?php echo base_url();?>YourControllerName/download">

(是的,你需要使用base_url的url助手)或在一些javascript onClick调用中使用该url,但我不确定这是否是最适合你的解决方案。

首先参考文档总是一个好主意:

http://codeigniter.com/user_guide/general/urls.htmlhttp://codeigniter.com/user_guide/helpers/url_helper.html

请注意使用AJAX调用。网上有一些很棒的资源,下面是其中之一:

http://www.switchonthecode.com/tutorials/simple-ajax-php-and-javascript