Faryal Khan
Faryal Khan

Reputation: 869

Calling a controller method from view

I have a method in my 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);

}

In my view I have a link

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

I want to call the download metho of my controller onclick event of the link . Is it good to do so ? How can I do it ? Thanks

Upvotes: 1

Views: 1678

Answers (3)

Philip Bevan
Philip Bevan

Reputation: 347

Please look in to using AJAX calls. There are some great resources out there on the web and here is one of them:

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

Upvotes: 0

jco
jco

Reputation: 677

You could try:

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

(and yes, you would need to be using the url helper for base_url) or use that url in some javascript onClick call, but I am unsure if this is the best solution for you.

Upvotes: 2

King Julien
King Julien

Reputation: 11308

It's always a good idea to refer to a documentation first:

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

Upvotes: 0

Related Questions