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 ~~~