Wana在codeigniter中使用Ajax上传多个图像,但在调用函数upload_business_photo_do()时遇到了一些错误请帮助
提前感谢....
<input type="button" id="uploadBusinessImg" value="Upload">
Ajax代码: - 我在这里通过id="uploadBusinessImg"
<script>
$("#uploadBusinessImg").on("click",function(e)
{
var total_img=$("#txtBusinessImage").get(0).files.length;
if(total_img<=5)
{
var f_size=0;
var f_size_1=0;
var f_type_1="" ,validFileName="",InValidFileName="";
var chkFileValid=true;
for(var i=0; i<=total_img-1; i++)
{
var f_name=$("#txtBusinessImage").get(0).files.item(i).name;
var f_size=$("#txtBusinessImage").get(0).files.item(i).size;
var f_type=f_name.split('.').pop();
//alert(f_type);
var valid_extensions = /(\.jpg|\.jpeg|\.png)$/i;
if(valid_extensions.test(f_name))
{
if(validFileName=="")
validFileName =f_name;
else
validFileName+=","+f_name;
}
else
{ InValidFileName=f_name;
chkFileValid=false;
break;
}
var newFileSize=parseInt(f_size);
f_size_1=parseInt(f_size_1)+parseInt(newFileSize);
}
var totalFileSize=f_size_1;
//alert(totalFileSize);
if(totalFileSize<=1572864 && chkFileValid==true)
{
//alert("allow");
alert(validFileName);
var businessIMAGE=validFileName;
$.ajax({
type:"POST",
url:"<?php echo site_url('Main_ctrl/upload_business_photo_do'); ?>",
mimeType:"multipart/form-data",
uploadMultiple: true,
data:{reciveBusinessImg:businessIMAGE},
success: function(reviceUploadImgMsg)
{
alert(reviceUploadImgMsg);
}
});
}
else
{
if(chkFileValid==false)
alert("InValidFileName"+InValidFileName);
else
alert("Image size should be less then 1.5 MB");
}
}
else
{
alert("Not Allow");
}
});
</script>
这是控制器代码: -
function upload_business_photo_do()
{
$reciveBusinessImgName=$this->input->post('reciveBusinessImg');
$newArray=explode(",",$reciveBusinessImgName);
/*code for image*/
$config['upload_path']='./company_image/';
$config['allowed_types']= 'jpg|png|jpeg';
$config['max_width'] = '6000';
$config['max_height'] = '4500';
$this->load->library('upload',$config);
for($i=0; $i<count($newArray); $i++)
{
$_FILES['userfile']['name']= $_FILES['txtBusinessImage']['name'][$i];
$_FILES['userfile']['type']= $_FILES['txtBusinessImage']['type'][$i];
$_FILES['userfile']['tmp_name']= $_FILES['txtBusinessImage']['tmp_name'][$i];
$_FILES['userfile']['error']= $_FILES['txtBusinessImage']['error'][$i];
$_FILES['userfile']['size']= $_FILES['txtBusinessImage']['size'][$i];
if(! $this->upload->do_upload())
{
/*----set flash message*/
echo "error";
}
else
{
$upload_data = $this->upload->data();
echo "done";
}
}
}