简体   繁体   中英

How to upload image from a dynamically Generated Form in CodeIgniter using ajax

I want to upload an image to a local folder using jquery ajax. The complex part is i have the forms which are generated dynamically, and to those forms and fields i am giving the id to it so as to show which form is submitted as shown below. I am using following code but the image is not uploaded.

View: Upload_View.php

<script type="text/javascript">
    function sendVideoData(frm_id)
    {    
        var data = new FormData(document.getElementById("post_video_"+frm_id));
        // make the AJAX request
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>"+"dashboard/do_upload",
            data: data+'&form_id='+frm_id,
            mimeType:"multipart/form-data",
            contentType: false,
            cache: false,
            processData:false,
            dataType: 'json',
            success: function (data) {
                alert("data"+data);
            },            
        });        
        return false;
    }
</script>

<form name="post_video" id="post_video_<?=$row1['id']?>" method="post" onsubmit="return sendVideoData(<?=$row1['id']?>)">           
    <input type="file" name="save_movie_<?=$row1['id']?>" id="movie_<?=$row1['id']?>" /> 
    <input name="type_lecture_id" class="get_lecture_id" id="get_lecture_id_<?=$row1['id']?>" value="<?=$row1['id']?>" type="hidden"/>
    <input type="button" class="postbtn" id="submit_movie_<?=$row1['id']?>" value="Upload Video File"/>
</form>

Controller:

$formid=$_POST['form_id']; 

$filename='save_movie_'.$formid;
$path_parts = pathinfo($_FILES[$filename]["name"]);
$extension = $path_parts['extension'];
$Random_file_name=$filename.".".$extension;
move_uploaded_file($_FILES[$filename]['tmp_name'], "http://localhost/dummy/uploads/".$Random_file_name);

save_movie_ this is the id of the file control and $formid shows from which form and which field we have to take data, but because i am new to Codeigniter i don't know from how to upload it. One thing more i want to display progress bar also to display the progress of image upload.Please reply. Thanks...

You must have to modify your jQuery ajax function in the following way

   var data = new FormData(document.getElementById("post_video_"+frm_id)[0]);
   jQuery("#progressbar").show(); // add your prgress bar here like this
   jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>"+"dashboard/do_upload",
        data: data+'&form_id='+frm_id,
        mimeType:"multipart/form-data",
        contentType: false,
        async: false,
        cache: false,
        processData:false,
        dataType: 'json',
        success: function (data) {
            alert("data"+data);
            jQuery("#progressbar").hide(); //hide your loader like this
        },            
    });        
    return false;

Thank You.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM