1.使用ajax
function insertTitle(tValue){
if(tValue!=''){
var len = tValue.length;
var str = tValue.substr(tValue.lastIndexOf("."),len);
if(str=='.jpg'||str=='.JPG'||str=='.gif'||str=='.GIF'||str=='.png'||str=='.PNG'||str=='.bmp'||str=='.BMP'){
}else{
parent.ymPrompt.alert({title:'信息提示',message:'请选择格式为 .jpg .JPG .gif .GIF .png .PNG .bmp .BMP的附件上传!',width:350,height:200});
return false;
}
var file = document.getElementById("files");
file.select();
document.getElementById("picDiv").focus();
var realPath = document.selection.createRange().text;
document.getElementById("tupianpath").value = realPath;
}
}
相应的jsp是:
<input type="file" name="files" id="file1" style="cursor: pointer;position:absolute;filter:alpha(opacity=0);width:0;opacity: 0.0;/**" size="1" οnchange="if(this.value)insertPhoto(this.value,'1');" hidefocus/>
2.不使用ajax
function fileChange(target){
//检测上传文件的类型
var ext,idx;
if (target == ''){
ymPrompt.alert({title:'信息提示',message:'请选择需要上传的文件!',width:250,height:160});
return;
} else {
idx = target.lastIndexOf(".");
if (idx != -1){
ext = target.substr(idx+1).toUpperCase();
ext = ext.toLowerCase( );
if (ext!='jpg'&&ext!='png'&&ext!='jpeg'&& ext!='gif'){
ymPrompt.alert({title:'信息提示',message:'只能上传jpg,png,jpeg,gif类型的文件!',width:250,height:160});
document.getElementById("yingye").innerHTML="<input name='file' id='yingyezhizhao' class='text_172' type=file οnchange=\"fileChange(this.value);\">";
return;
}
} else {
return;
}
}
}
</script>
相应的jsp为:
<input name='file' id='yingyezhizhao' type=file class="text_172" οnchange="fileChange(this.value);" accept=".gif,.jpg,.jpeg,.png">