php - 从 View 调用 Controller 方法

标签 php javascript codeigniter

我的 Controller 中有一个方法

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="???">

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

最佳答案

你可以试试:

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

(是的,您需要为 base_url 使用 url 帮助程序)或在某些 javascript onClick 调用中使用该 url,但我不确定这是否是最适合您的解决方案。

关于php - 从 View 调用 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789671/

相关文章:

php - 使用 codeigniter 在 url 中使用 base64 编码的字符串

php - 如何同时运行多个SQL查询?

php - 在不知道字段名称的情况下选择主键值

javascript - 动态创建编辑器 - 浏览器是否需要退出该功能才能进行更改?

javascript - Vee-Validate validateAll() with scope

php - 缺少函数变量时如何重定向

php - 与 CodeIgniter 中的 GROUP_CONCAT 相呼应的多行数据总和

php - 如何在 symfony2 中使用具有多个不同表单小部件的实体字段类型数组

php - Symfony 2 Formtype 查询生成器

javascript - puppeteer 师 + 像素匹配 : Comparing screenshots in Mac and TravisCI Linux