Copy codeThe Code is as follows: <script>
Function setImagePreview (){
Var docObj = document. getElementById ("doc ");
Var imgObjPreview = document. getElementById ("preview ");
If (docObj. files & docObj. files [0]) {
// Set the img attribute directly under Firefox
ImgObjPreview. style. display = 'block ';
ImgObjPreview. style. width = '300px ';
ImgObjPreview. style. height = '120px ';
// ImgObjPreview. src = docObj. files [0]. getAsDataURL ();
// Firefox 7 or later versions cannot be obtained using the getAsDataURL () method.
ImgObjPreview. src = window. URL. createObjectURL (docObj. files [0]);
} Else {
// Use the filter in IE
DocObj. select ();
Var imgSrc = document. selection. createRange (). text;
Var localImagId = document. getElementById ("localImag ");
// The initial size must be set.
LocalImagId. style. width = "250px ";
LocalImagId. style. height = "200px ";
// Capture Abnormal images to prevent users from modifying suffixes to forge Images
Try {
LocalImagId. style. filter = "progid: DXImageTransform. Microsoft. AlphaImageLoader (sizingMethod = scale )";
LocalImagId. filters. item ("DXImageTransform. Microsoft. AlphaImageLoader"). src = imgSrc;
} Catch (e ){
Alert ("the format of the image you uploaded is incorrect. Please select another one! ");
Return false;
}
ImgObjPreview. style. display = 'none ';
Document. selection. empty ();
}
Return true;
}
</Script>
<Body>
<Input type = file name = "doc" id = "doc" onchange = "javascript: setImagePreview ();">
<P> <div id = "localImag"> </div> </p>