Upload pictures to a preview of the picture, you can understand the image upload will probably look like, this function with JS implementation, and then in the FileUpload control of the Change event call, so when the FileUpload select the picture, the picture will automatically show up. The function is very simple, but very practical.
Preview The JS code of the picture:
Copy Code code as follows:
<script type= "Text/javascript" >
function Setimagepreview (Docobj,localimagid,imgobjpreview)
{
if (Docobjfiles && docobjfiles[0])
{
Firefox, set the IMG property directly
Imgobjpreviewstyledisplay = ' block ';
Imgobjpreviewstylewidth = ' 300px ';
Imgobjpreviewstyleheight = ' 200px ';
Firefox 7 version can not be used above the Getasdataurl () way to get, need a way
IMGOBJPREVIEWSRC = Windowurlcreateobjecturl (Docobjfiles[0]);
}
Else
{
ie, using filters
Docobjselect ();
var imgsrc = Documentselectioncreaterange () text;
Initial size must be set
Localimagidstylewidth = "300px";
Localimagidstyleheight = "200px";
Image exception capture to prevent users from modifying the suffix to forge pictures
Try
{
Localimagidstylefilter= "Progid:dximagetransformmicrosoftalphaimageloader (Sizingmethod=scale)";
Localimagidfiltersitem ("Dximagetransformmicrosoftalphaimageloader") src = imgsrc;
}
catch (E)
{
Alert ("The image you uploaded is not in the correct format, please choose again!");
return false;
}
Imgobjpreviewstyledisplay = ' None ';
Documentselectionempty ();
}
return true;
}
</script>
FileUpload control and image used to preview a picture:
Copy Code code as follows:
<div id= "Localimag" style= "width:300px; height:200px ">
</div>
<asp:fileupload id= "Picload" width= "300px" runat= "Server" onchange= "Javascript:setimagepreview" (This,localImag, Preview); " >
</asp:FileUpload>
function is very simple, the code is also very easy to understand, I believe that I do not have much nonsense we can see it. Feel writing such an article, really have nothing to say, not much, or focus on the code.