<script>
function setImagePreview() {
var docObj=documentgetElementById("doc");
var imgObjPreview=documentgetElementById("preview");
if(docObjfiles && docObjfiles[]){
//火狐下直接設img屬性
imgObjPreviewstyledisplay = block;
imgObjPreviewstylewidth = px;
imgObjPreviewstyleheight = px;
//imgObjPreviewsrc = docObjfiles[]getAsDataURL();
//火狐以上版本不能用上面的getAsDataURL()方式獲取需要一下方式
imgObjPreviewsrc = windowURLcreateObjectURL(docObjfiles[]);
}else{
//IE下使用濾鏡
ocObjselect();
var imgSrc = documentselectioncreateRange()text;
var localImagId = documentgetElementById("localImag");
//必須設置初始大小
localImagIdstylewidth = "px";
localImagIdstyleheight = "px";
//圖片異常的捕捉防止用戶修改後綴來偽造圖片
try{
localImagIdstylefilter=
"progid:DXImageTransformMicrosoftAlphaImageLoader(sizingMethod=scale)";
localImagIdfiltersitem
("DXImageTransformMicrosoftAlphaImageLoader")src = imgSrc;
}catch(e){
alert("您上傳的圖片格式不正確請重新選擇!");
return false;
}
imgObjPreviewstyledisplay = none;
documentselectionempty();
}
return true;
}
</script>
<body>
<input type=file name="doc" id="doc" onchange="javascript:setImagePreview();">
<p><div id="localImag">
<img id="preview" width= height= style="diplay:none" />
</div></p>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20450.html