gpt4 book ai didi

php - 如何使用 ajax 从 CodeIgniter 中动态生成的表单上传图像

转载 作者:行者123 更新时间:2023-12-01 02:53:35 25 4
gpt4 key购买 nike

我想使用 jquery ajax 将图像上传到本地文件夹。复杂的部分是我有动态生成的表单,并且对于这些表单和字段,我为其提供 id,以便显示提交了哪个表单,如下所示。我正在使用以下代码,但图像未上传。

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_ 这是文件控件的 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;

谢谢。

关于php - 如何使用 ajax 从 CodeIgniter 中动态生成的表单上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33233246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com