Home > Web Front-end > JS Tutorial > body text

FileReader implements local preview before uploading pictures

小云云
Release: 2018-03-03 09:10:19
Original
1530 people have browsed it

Usually when doing image upload preview, if there are no special requirements, just transfer the image directly to the background. After success, get the URL and then render it on the page. This is not a problem when the image is relatively small. If it is larger, it will be fine. It will be slower to see the preview, and junk files will be generated, so it is better to preview it locally before uploading.

When I was working on a project and looking for a plug-in, I knew that the pure front-end could realize local preview of images, but when I was asked about it during the interview today, I looked confused, and then I accidentally discovered it on the computer desktop. I have implemented the demo, and then checked the API based on the demo, and summarized it briefly:

First you have to get the File object

When uploading a picture with the input tag, the event object will contain the file object A collection

event.target.files

The core is the FileReader object

According to MDN:

The FileReader object allows web applications to read asynchronously The contents of a file (or raw data buffer) stored on the user's computer. Use a File or Blob object to specify the file or data to be read.

First, you need to get an instance object of FileReader as a constructor

var freader = new FileReader();
Copy after login

Use the readAsDataURL() method to read the specified content

freader.readAsDataURL(file);
Copy after login

The last thing is an event processing, which is equivalent to monitoring Reading progress, here we render the image when the reading is completed, so use onload

freader.onload = function(e) { console.log(e); myImg.setAttribute('src', e.target.result); }
Copy after login

After the frame loading is completed, what we finally get is a base64-encoded src address. The specific reason will be checked next time. Write an article about base64 encoding

Complete code

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <form action=""> 
 <input type="file" name="files" id="fileTog" accept="image/*" multiple="multiple" onchange="changImg(event)"> 
 <img alt="暂无图片" id="myImg" src="" height="100px" width="100px"> 
 </form>
 <script>
 function changImg(e){ 
 var myImg = document.getElementById('myImg');
 for (var i = 0; i < e.target.files.length; i++) { 
 var file = e.target.files[i]; 
 console.log(file); 
 if (!(/^image\/.*$/i.test(file.type))) { 
  continue; //不是图片 就跳出这一次循环 
 } 
 //实例化FileReader API 
 var freader = new FileReader(); 
 freader.readAsDataURL(file); 
 freader.onload = function(e) { 
  console.log(e);
  myImg.setAttribute(&#39;src&#39;, e.target.result); 
 } 
 } 
 }
 </script>
</body>
</html>
Copy after login

Postscript

Through this incident, I exposed a problem in learning new things, which is to check it out and read it again. As long as you know it, this habit is particularly harmful. In the future, whenever you have a question, you should not only check how it is done, but also understand why it is done. The so-called knowing what it is and why it is done. Also, you can type the code with your hands and try not to copy it. Copying it is fun for a while, but it is embarrassing to not be able to write it by hand.

Today is the third day in Hangzhou and the second day of interviews. An important problem exposed in these two days of interviews is that I usually rely too much on search engines and use too little brain. Even some simple APIs can’t be used. If you don’t remember everything, there are indeed many things in the front end that you need to keep in mind. There are no shortcuts. These things are the basics. If you don’t remember them, you have a poor foundation. Although it doesn't affect your ability to make things, it will affect development efficiency. For technology to move forward, this cornerstone must be stable, so keep it in mind!

Related recommendations:
How does H5’s file domain FileReader read files in segments and upload them to the server

JavaScript uses FileReader to achieve image upload preview effects

Parsing the usage of FileReader interface in HTML5

The above is the detailed content of FileReader implements local preview before uploading pictures. 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