jsp页面代码
<input id="uploadImg" type="file" class="form-control" οnchange="fileChange(this);" name="thumbnail">
js代码
function fileChange(target) {
$("#linkThumbnail").html("");
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if (isIE && !target.files) {
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if(size>100){
alert("附件不能大于100kb");
target.value="";
return
}
var name=target.value;
var fileName = name.substring(name.lastIndexOf(".")+1).toLocaleUpperCase();
if(fileName !="PNG" && fileName !="JPEG" && fileName !="GIF"){
alert("请选择PNG、JPEG、GIF格式文件上传!");
target.value="";
return
}
var file = target.files[0];
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
var cacheHtml =null;
if(url!= null){
cacheHtml = "<img class='img-responsive ' src='"+url+"'/>";
}
$("#linkThumbnail").append(cacheHtml);
}