JS Upload picture Preview

Source: Internet
Author: User

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

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.