![](/img/trans.png)
[英]How to upload image using ajax and codeigniter without resetting the form?
[英]How to upload image from a dynamically Generated Form in CodeIgniter using ajax
我想使用jquery ajax将图像上传到本地文件夹。 复杂的部分是我具有动态生成的表单,我为这些表单和字段赋予了ID,以显示提交的表单,如下所示。 我正在使用以下代码,但未上传图片。
查看: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>
控制器:
$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_这是文件控件的ID,$ formid显示我们必须从哪个表单和哪个字段获取数据,但是由于我是Codeigniter的新手,所以我不知道如何上传它。 我还想显示进度栏,以显示图像上传的进度。请回复。 谢谢...
您必须通过以下方式修改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;
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.