15 November 2009 7 Comments

Codeigniter Ajax File Upload

Codeignier Ajax File Upload module is easy and simple and build with Codeigniter File Uplaod Library and  AjaxFileUpload – Jquery Plugin .

File: ajaxupload.php (Contorller).

function doupload(){

$uploadFile = uri_assoc(‘fld’,2);
$config['upload_path'] = ‘./uploads/’;
$config['allowed_types'] = ‘gif|jpg|png’;
$this->load->library(‘upload’, $config);

if ( ! $this->upload->do_upload($uploadFile))
{
$error = array(‘error’ => $this->upload->display_errors());

$this->load->view(‘upload_form’, $error);
}
else
{
$data = array(‘upload_data’ => $this->upload->data());

$this->load->view(‘upload_success’);
}

}

File: upload_form.php(views).

<script type=”text/javascript” src=”<?=base_url()?>/ajaxfileupload.js”></script>

<input type=”file” name=”uploadfile”  id=”uploadfile” />
<intpy type=”button” name=”upload” id=”upload”  value=”Upload” />

<script>

$(function(){
$(‘#upload ‘).click(function(){

$.ajaxFileUpload
(
{

url:  “ajaxupload/doupload/fld/uploadfile”,
secureuri:false,
fileElementId: ‘uploadfile’,
dataType: ‘json’,
success: function (data, status)
{

if(typeof(data.error) != ‘undefined’)
{
if(data.error != ”)
{
alert(data.error);
}else
{
alert(data.msg);
}
}

},
error: function (data, status, e)
{
alert(e);

}
}
);
});
});

</script>

Dowmlad ajaxfileuplaod.js .

VN:F [1.8.8_1072]
Rating: 7.5/10 (2 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)
Codeigniter Ajax File Upload7.5102

7 Responses to “Codeigniter Ajax File Upload”

  1. smaq 10 December 2009 at 12:10 am #

    Dear Author tandukar.com !
    I do not believe.

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: +1 (from 1 vote)
  2. Majitonic 25 December 2009 at 2:40 pm #

    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  3. admin 27 December 2009 at 9:44 am #

    Hi Majitonic you can twit me as “tandubhai” in twitter.
    Sure , you can quote in your blog.

    UA:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  4. moe 9 June 2010 at 10:21 am #

    you must echo json_encode($error) so the view can pick up the result

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: +1 (from 1 vote)
  5. Pratibhushan Neupane 14 June 2010 at 10:22 am #

    Dear sir.
    I think u know me.Plz write a code in codeignitor to upload multiple file with database insertion and updating part.

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  6. danjan 30 July 2010 at 12:47 am #

    Looks great but incomplete
    uri_assoc() ???
    That’s an undefined function – what is it?

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)
  7. vin 18 August 2010 at 2:58 am #

    Hi, am trying to implement your code. I get an error:

    syntax error: invalid token <

    do you have any idea why?

    UN:F [1.8.8_1072]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.8_1072]
    Rating: 0 (from 0 votes)

Leave a Reply