Home > Web Front-end > JS Tutorial > JavaScript uses FileReader to achieve image upload preview effect

JavaScript uses FileReader to achieve image upload preview effect

韦小宝
Release: 2018-01-25 11:02:08
Original
1661 people have browsed it

This article mainly introduces in detail JavaScriptUsing FileReader to achieve image upload preview effect, which has certain reference value for learning JavaScript. Friends who are interested in JavaScript can refer to this article

FileReader is part of the HTML5 File API. It implements an asynchronous file reading mechanism. You can think of FileReader as XMLHttpRequest, the only difference is that it reads the file system instead of the remote server. In order to read the data in the file, FileReader provides the following methods.

  • readAsText(file,encoding): Read the file in plain text and save the read file to the result attribute.

  • readAsDataURL(file): Read the file and save the file in the result attribute in the form of data URI.

  • readAsBinaryString(file): Read the file and save a string in the result attribute. Each character in the string represents one byte.

  • readAsArrayBuffer(file): Read the file and save an ArrayBuffer containing the contents of the file in the result attribute.

  • multiple attribute indicates support for multiple images


  • <p id="wrapper">    
     <input id="fileUpload" type="file" multiple /><br />
     <p id="image-holder"> </p>
    </p>
    Copy after login


$("#fileUpload").on(&#39;change&#39;, function () {
 
  //获取上传文件的数量
  var countFiles = $(this)[0].files.length;
 
  var imgPath = $(this)[0].value;
  var extn = imgPath.substring(imgPath.lastIndexOf(&#39;.&#39;) + 1).toLowerCase();
  var image_holder = $("#image-holder");
  image_holder.empty();
 
  if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
    if (typeof (FileReader) != "undefined") {
 
      // 循环所有要上传的图片
      for (var i = 0; i < countFiles; i++) {
 
        var reader = new FileReader();
        reader.onload = function (e) {
          $("<img />", {
            "src": e.target.result,
              "class": "thumb-image"
          }).appendTo(image_holder);
        }
 
        image_holder.show();
        reader.readAsDataURL($(this)[0].files[i]);
      }
 
    } else {
      alert("你的浏览器不支持FileReader!");
    }
  } else {
    alert("请选择图像文件。");
  }
});
Copy after login
FileReader can support Internet Explorer 10+, FireFox, Chrome and Opera browsers.

For students who are not familiar with JavaScript uploading, this article can be used to learn JavaScript uploading! !


Related recommendations:


JavaScript uses FileReader to complete image upload and preview function introduction

Native js FileReader Detailed introduction of the object

js example code for reading files through the filereader interface

The above is the detailed content of JavaScript uses FileReader to achieve image upload preview effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template