Pre-upload preview of images used in recent projects, compatible with IE6-9,FF
<body> <input type=file name= "Doc" id= "Doc" onchange= "Javascript:setimagepreview ();" >
Because there are multiple upload buttons in the project, you need to make some changes to the online example, actually using the code:
Copy Code
function Setimagepreview (fileobj, Previewobj, localimg) {
var Docobj=document.getelementbyid (fileobj), Var Imgobjpreview=document.getelementbyid (previewobj), if ( Docobj.files && Docobj.files[0]) {//Firefox, directly set img attribute ImgObjPreview.style.display = ' block '; ImgObjPreview.style.width = ' 120px '; ImgObjPreview.style.height = ' 80px '; IMGOBJPREVIEW.SRC = Docobj.files[0].getasdataurl (); Firefox 7 or above can not be obtained using the above Getasdataurl () method, need a way imgobjpreview.src = window. Url.createobjecturl (Docobj.files[0]);} else{//ie, use the filter Docobj.select (); var imgsrc = Document.selection.createRange (). text; var localimagid = document.getElementById (localimg); The initial size must be set localImagId.style.width = "120px"; LocalImagId.style.height = "80px"; Image exception capture to prevent users from modifying suffixes to forge pictures try{localimagid.style.filter= "Progid:DXImageTransform.Microsoft.AlphaImageLoader (sizing Method=scale) "; LocalImagId.filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src = imgsrc; }catch (e) {alert ("The image you uploaded is not in the correct format, please select it again!"); return false; } ImgObjPreview.style.display = ' none '; Document.selection.empty ();} return true;
}
JS Upload picture Preview