javascript上传图片实时更新 (js实现excel文件上传功能)

/*

图片预览

使用方法:

<form>

<div id="previewImg"><img id="previewImgSrc" src='' /></div>

<input type="file" id="upPreviewImg" name="fileimg" />

</form>

<style>

#previewImg{height:500px;}

#previewImgSrc{display:none; height:500px;}

</style>

*/

function img001(){

window.onload=function(){

var

isIE=function(){

return !!window.ActiveXObject;

},

isIE6=function(){

return isIE()&&!window.XMLHttpRequest;

},

isIE7=function(){

return isIE()&&!isIE6()&&!isIE8();

},

isIE8=function(){

return isIE()&&!!idocument.documentMode;

},

setCss=function(_this,cssOption){

if(!_this||_this.nodeType===3||_this.nodeType===8||!_this.style){

return;

}

for(var cs in cssOption){

_this.style[cs]=cssOption[cs];

}

return this;

},

upPreviewImg=function(options){

var _e=options.e,

preloadImg=null;

_e.onchange=function(){

var _v=this.value,

_body=document.body;

picReg=/(.JPEG|.jpeg|.jpg|.JPG|.GIF|.gif|.BMP|.bmp|.PNG|.png){1}/;

if(!picReg.test(_v)){

alert("请选择正确的图片格式");

return false;

}

if(typeof FileReader=='undefined'){

if(this.file){

options.previewImgSrc.setAttribute("src",this.file.files[0].getAsDataURL());

options.previewImgSrc.style.display="block";

}else if(isIE6()){

options.previewImgSrc.setAttribute("src",_v);

options.previewImgSrc.style.display="block";

}else{

_v=_v.replace(/[)'''%]/g,function(str){

return escape(escape(str));

});

setCss(

options.previewImgSrc,

{

"filter":"progid:DXImageTransForm.Microsft.AlphaImageLoader(sizingMethod='scale',src=\""+90+_v+"\")",

"display":"block"

}

);

options.previewImgSrc.setAttribute("src",(isIE6()||isIE7()?"!blankImage":"dataimage/gif;base64,R0IGODIhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="));

}

}else{

var reader=new FileReader(),

_file=this.files[0];

reader.onload=(function(file){return function(){

options.previewImgSrc.setAttribute("src",this.result);

options.previewImgSrc.style.display="block";

}})(_file);

reader.onerror=function(){

alert("文件读取数据出错");

}

reader.readAsDataURL(_file);

}

}

};

upPreviewImg({

"e":document.getElementById("upPreviewImg"),

"previewImgSrc":document.getElementById("previewImgSrc")

});

};//onload

}

js上传图片裁剪正方形,javascript上传图片实时更新

假装有图

也可以改为单独的函数调用。