Examples of js Image Upload and preview functions, and examples of js Image Upload
This document describes how to upload images and preview images in JavaScript. Share it with you for your reference. The specific analysis is as follows:
According to some people's code on the internet, I wrote a function to upload images for timely preview.
<input type = "file"/> function DisplayImage (fileTag, imgTagId) {var allowExtention = ".jpg.png.gif"; var extentionArr = fileTag. value. split ('. '); var extention = extentionArr [extentionArr. length-1]; if (! (AllowExtention. indexOf (extention)>-1) {alert ("Please upload image! ");} Else {// for adveced broswer (the newest ie, chrome, ff) if (typeof (FileReader )! = "Undefined") {var reader = new FileReader (); reader. readAsDataURL (fileTag. files [0]); reader. onload = function (e) {document. getElementById (imgTagId ). setAttribute ("src", e.tar get. result) ;}} else {// for (ie6) document. getElementById (imgTagId ). setAttribute ("src", fileTag. value );}}}
I hope this article will help you design javascript programs.