Upload image preview JS script Input file image preview implementation example, js image Preview

Source: Internet
Author: User

Upload image preview JS script Input file image preview implementation example, js image Preview

When working on a project in Shenzhen, a user needs to upload the Avatar preview function! I found a lot on the Internet, but I am not satisfied with it. Either flash, or the path of the image returned after Ajax upload, or it cannot be used at all. Fortunately, someone previously wrote an image preview function in this project. I also turned it out and made a record here to facilitate future use, it is also convenient for other friends who need it!

The Code is as follows:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Test can be used in IE8, FF12.0, and Google chrome 28.0.1500.72!


JS compatible with the local preview code for uploading images from IE8

It must be your problem, the js path is incorrect, and so on.

<Script>
Var version = parseFloat (navigator. appVersion. split ("MSIE") [1]);
Function yulan (file, id ){
If (file. value. indexOf (". jpg ") <0 & file. value. indexOf (". jpeg ") <0 & file. value. indexOf (". gif ") <0 & file. value. indexOf (". png ") <0 & file. value. indexOf (". JPG ") <0 & file. value. indexOf (". JPEG ") <0 & file. value. indexOf (". GIF ") <0 & file. value. indexOf (". PNG ") <0 ){
Alert ('the image file you selected is not ');
Return false;
}
If (navigator. userAgent. indexOf ("Mozilla/5.")>-1 ){
Document. getElementById (id). innerHTML = " ";
CheckFileSize (file. files [0]. getAsDataURL (), id); // if (CheckFileSize (file. files [0]. getAsDataURL ()> 328) document. getElementById (id ). getElementsByTagName ('img ') [0]. style. width = "328px ";

} Else {
File. select ();
Var img = document. selection. createRange (). text;
Document. getElementById (id). innerHTML = " ";
If (CheckFileSize (file. value)> 328) document. getElementById (id). getElementsByTagName ('img ') [0]. style. width = "328px ";
}
}
</Script>
<Div id = "tu"> </div>
<Form name = "form1" method = "POST" enctype = "multipart/form-data">
<Input type = "file" n ...... remaining full text>

How does JS achieve image preview?

<Form action = "" method = "post" enctype = "multipart/form-data" name = "form1">

<Br/>
<Input name = "picture" type = "file" id = "picture" onchange = "img. src = this. value"/>
</Form>

You can directly trigger the onchange event without a button ~~~

Related Article

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.