关于图片上传现在比较流行点是flash上传,但在网络不给力或某些浏览器下flash就不能用了,所以各大网站不管flash上传做的多炫,都会保留最原始的图片上传功能通常叫做“普通上传”。
<form method="post" name="formupload" target="normalupload" enctype="multipart/form-data"action="/util/image/_upload">
<input type="file" name="upload" value="" ><span></span>
<input type="submit" />
</form>
<iframe scrolling="auto" id="iframe_data" frameborder="no" marginheight="0" style="display:none;"name="normalupload"></iframe>
在form上加target=" normalupload ",这样就ok了,其实很简单滴,点提交后用firebug可以看到返回的数据都在那个隐藏的iframe里。
iframe.on('load',function(){
//处理返回数据
//html为返回的数据
var doc = iframe.contentWindow.document,
html = doc.body.innerHTML;
if(html!=''){
//TO DO other
}
});
var changeFun =function(){
var val =this.value,//获取图片名称
imgName = val.substring(val.lastIndexOf('.')+1,val.length),//图片格式
name = imgName.toLowerCase(),
spanNode = H.ui(this.parentNode).tag('span')[0];
if(name!='jpg'&&name!='jpeg'&&name!='gif'&&name!='png'){//验证图片格式
spanNode.innerHTML ='图片格式不对,请重新选择!';
spanNode.className ='err';
this.value ='';
}else{
if(spanNode.innerHTML !=''){
spanNode.innerHTML ='';
spanNode.className ='';
}
var forms = pannel.tag('form'),len = forms.length;
if(len >=5){//验证form长度,最多添加5个
return;
}
var oldnode = pannel.tag('form')[0];
var newnode =oldnode.cloneNode(true);
if(oldnode.action.indexOf('rules')==-1){
newnode.action += rules;
}
var input = H.ui(newnode).tag('input');
input[0].value ='';
_pannel.appendChild(newnode);//插入form
input[0].onchange = changeFun;//为新的input绑定onchange事件
}
};
normal_upload.tag('input')[0].onchange = changeFun;
//提交表单
function submitform(success){
var forms = pannel.tag('form'),
len = forms.length;
isSubmit =true,
spanNote = pannel.tag('span');
if(typeof success =='boolean'){
if(success){
spanNote[uploadnum].innerHTML ='上传成功';
}else{
spanNote[uploadnum].innerHTML ='上传失败';
}
uploadnum++;
}
if(uploadnum >= len){
returntrue;
}
var imgVal = H.ui(forms[uploadnum]).tag('input')[0].value;
if(imgVal ==''){
isSubmit =false;
}else{
spanNote[uploadnum].innerHTML ='上传中...';
}
if(!isSubmit){
returntrue;
}
forms[uploadnum].submit();
returnfalse;
};