简体   繁体   English

如何使用Ajax从CodeIgniter中的动态生成的表单上载图像

[英]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. 我想使用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. 复杂的部分是我具有动态生成的表单,我为这些表单和字段赋予了ID,以显示提交的表单,如下所示。 I am using following code but the image is not uploaded. 我正在使用以下代码,但未上传图片。

View: Upload_View.php 查看: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. save_movie_这是文件控件的ID,$ formid显示我们必须从哪个表单和哪个字段获取数据,但是由于我是Codeigniter的新手,所以我不知道如何上传它。 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 您必须通过以下方式修改jQuery ajax函数

   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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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