Use HTML5 to Easily preview images-

Source: Internet
Author: User
The image upload function is implemented on the webpage. After you select an image file, you can easily implement this function through FileReader since HTML5 is available .,. Implement in Web pages Image Upload FunctionAfter you select an image file and want to preview the image on the page, this simple requirement can be implemented a long time ago, later, due to security issues, direct access to local files is forbidden. For a long time, it is impossible to preview the selected image through HTML, this function is back, and can be easily implemented through FileReader.

You only need to listen to the onchange event in the <input type = "file"/> file form element, read the image file through FileReader, and then display the read content in. The sample code is as follows:

Document. getElementById ('file '). onchange = function (evt) {// if the browser does not support FileReader, if (window. fileReader) return; var files = evt.tar get. files; for (var I = 0, f; f = files [I]; I ++) {if (! F. type. match ('image. * ') {continue;} var reader = new FileReader (); reader. onload = (function (theFile) {return function (e) {// img element document. getElementById ('previewimage '). src = e.tar get. result ;};}) (f); reader. readAsDataURL (f );}}

The above figure shows how to use HTML5 to Easily preview images. For more information, see the PHP Chinese website (www.php1.cn )!

Related Articles:

Html5 image uploads support image preview compression and progress display compatible with IE6 and standard browsers

Javascript for image preview and upload (compatible with IE) code sharing

JavaScript advanced (8) JavaScript to implement image preview and import server functions

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.