Javascript 使用Jquery进行两次图像上传
我已经有图像上传代码,这是工作良好,它只有一个图像上传选项在它 此处,图像被发送到函数,在该函数中,图像将上载并返回图像src代码和图像路径,一旦返回,将在Javascript 使用Jquery进行两次图像上传,javascript,php,jquery,html,laravel,Javascript,Php,Jquery,Html,Laravel,我已经有图像上传代码,这是工作良好,它只有一个图像上传选项在它 此处,图像被发送到函数,在该函数中,图像将上载并返回图像src代码和图像路径,一旦返回,将在$('#image').html(数据)中填充图像 当我有一个选项时效果很好,而我有两个上传选项,但失败了 我所面临的错误是,它在第一种情况下起作用 <input type="file" id="image" name="image" class="upload-img" style="opacity:0"> 以下是脚本: &l
$('#image').html(数据)中填充图像代码>
当我有一个选项时效果很好,而我有两个上传选项,但失败了
我所面临的错误是,它在第一种情况下起作用
<input type="file" id="image" name="image" class="upload-img" style="opacity:0">
以下是脚本:
<script>
$(document).ready(function()
{
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: 'globalimageupload',
type: 'POST',
data: formData,
async: true,
success: function (data)
{
$('#image').html(data);
console.log(data);
},
cache: false,
contentType: false,
processData: false
});
return false;
});
$("input[type='file']").on("change", function()
{
$("form#data").submit();
});
});
</script>
$(文档).ready(函数()
{
$(“表单数据”).submit(函数(){
var formData=新formData($(此)[0]);
$.ajax({
url:“globalimageupload”,
键入:“POST”,
数据:formData,
async:true,
成功:功能(数据)
{
$('#image').html(数据);
控制台日志(数据);
},
cache:false,
contentType:false,
processData:false
});
返回false;
});
$(“input[type='file'])。在(“change”,function()上)
{
$(“表格数据”).submit();
});
});
以下是图像句柄函数:
public function globalimageupload()
{
$file = Input::file('image');
if (Input::file('image'))
{
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2000 * 1024; // max file size (200kb)
$ext = $file->guessClientExtension();
$size = $file->getClientSize();
if (in_array($ext, $valid_exts) AND $size < $max_size)
{
$image=Input::file('image');
$destinationPath = public_path()."/assets/uploads/companylogo/";
$num_unique = md5(uniqid() . time());
$fileName=$num_unique.'.'.$ext;
Input::file('image')->move($destinationPath,$fileName);
$desPathimg=public_path()."assets/uploads/companylogo/".$fileName;
$desPath=$fileName;
return HTML::image('assets/uploads/companylogo/'.$fileName,'photo', array( 'width' => 128, 'id'=> 'po', 'name'=> 'po', 'height' => 128 )).'<input type="hidden" name="imagetextbox" id="imagetextbox" value="'.$desPath.'">';
}
else
{
return 'Check the Extension and file size';
}
}
else
{
return "Please upload any Image";
}
}
公共函数globalimageupload()
{
$file=Input::file('image');
if(输入::文件('image'))
{
$valid_exts=array('jpeg','jpg','png','gif');//有效扩展名
$max_size=2000*1024;//最大文件大小(200kb)
$ext=$file->guessClientExtension();
$size=$file->getClientSize();
if(在数组中($ext,$valid_exts)和$size<$max_size)
{
$image=Input::file('image');
$destinationPath=public_path()。“/assets/uploads/companylogo/”;
$num_unique=md5(uniqid().time());
$fileName=$num_unique.'.$ext;
输入::文件('image')->move($destinationPath,$fileName);
$desPathimg=public_path().“assets/uploads/companylogo/”$fileName;
$desPath=$fileName;
返回HTML::image('assets/uploads/companyloads/'.$fileName,'photo',array('width'=>128,'id'=>'po','name'=>'po','height'=>128));
}
其他的
{
返回“检查扩展名和文件大小”;
}
}
其他的
{
返回“请上传任何图片”;
}
}
我所犯的错误是什么?如何修复此错误?在绑定提交事件之前使用unbind函数,如下所示:
$("input[type='file']").on("change", function(){
$("form#data").unbind('submit');
$("form#data").submit();
});
当您一次又一次地绑定事件(即此处为submit)时发生。在页面加载中,您绑定了提交事件,再次绑定了相同的输入更改事件,因此代码将执行两次并上载两次文件。但是取消绑定提交让我直接执行提交操作,然后我添加了两个函数,如下$(document)。on(“change”,“#imageone”,function(){$(“form#dataone”).submit();}),但它没有调用第二个函数
public function globalimageupload()
{
$file = Input::file('image');
if (Input::file('image'))
{
$valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$max_size = 2000 * 1024; // max file size (200kb)
$ext = $file->guessClientExtension();
$size = $file->getClientSize();
if (in_array($ext, $valid_exts) AND $size < $max_size)
{
$image=Input::file('image');
$destinationPath = public_path()."/assets/uploads/companylogo/";
$num_unique = md5(uniqid() . time());
$fileName=$num_unique.'.'.$ext;
Input::file('image')->move($destinationPath,$fileName);
$desPathimg=public_path()."assets/uploads/companylogo/".$fileName;
$desPath=$fileName;
return HTML::image('assets/uploads/companylogo/'.$fileName,'photo', array( 'width' => 128, 'id'=> 'po', 'name'=> 'po', 'height' => 128 )).'<input type="hidden" name="imagetextbox" id="imagetextbox" value="'.$desPath.'">';
}
else
{
return 'Check the Extension and file size';
}
}
else
{
return "Please upload any Image";
}
}
$("input[type='file']").on("change", function(){
$("form#data").unbind('submit');
$("form#data").submit();
});