<form name="upform" action="UploadServlet" encType="multipart/form-data" method="post" target="hideUpload" />
<input type="file" name="fileField" id="fileField" size="20"/>
<input type="submit" id="uploadfile" value="上传" class="smallButton" οnclick='LimitAttach(this.form, this.form.fileField.value);'/>
extArray = new Array(".jpg", ".png", ".jpeg");//允许的文件类型
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1); // 取文件名
ext = file.slice(file.indexOf(".")).toLowerCase();//取文件后缀名
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) {
form.submit();
alert(file+" 上传成功!");
}
else
alert("只能上传: "
+ (extArray.join(" ")) + "\n请重新选择文件"
+ "再上传.");
}
<input type="file" name="fileField" id="fileField" size="20"/>
<input type="submit" id="uploadfile" value="上传" class="smallButton" οnclick='LimitAttach(this.form, this.form.fileField.value);'/>
extArray = new Array(".jpg", ".png", ".jpeg");//允许的文件类型
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1); // 取文件名
ext = file.slice(file.indexOf(".")).toLowerCase();//取文件后缀名
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) {
form.submit();
alert(file+" 上传成功!");
}
else
alert("只能上传: "
+ (extArray.join(" ")) + "\n请重新选择文件"
+ "再上传.");
}