JS upload picture before uploading the image of the preview _javascript tips

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.