Home >Web Front-end >JS Tutorial >How to preview images locally when uploading them
This time I will show you how to preview locally first when uploading pictures, and what are the precautions to implement local preview when uploading pictures. The following is a practical case, let's take a look.
The FileReader object allows a Web application to asynchronously read the contents of a file (or raw data buffer) stored on the user's computer, using a File or Blob object to specify the file or data to be read. This article will introduce to you the use of FileReader in JS to realize the local preview function before uploading pictures. Friends who need it can refer to the introduction
What I usually do When uploading and previewing images, if there are no special requirements, just upload the image directly to the background. After success, get the URL and then render it on the page. This will not cause any problem when the image is relatively small. If it is larger, it will be slower to view. It’s time to preview, and junk files are generated, so it’s 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 was confused, and then I accidentally discovered the implementation on the computer desktop. demo, and then checked the API based on the demo, and briefly summarized it:
First you have to get the File object
When using input TagWhen uploading pictures, the event object will contain a collection of file objects
##The core is the FileReader object
According to MDN: The FileReader object allows a web application to asynchronously read 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 read.
First, as aconstructor
Get an instance object of FileReadervar freader = new FileReader();Use the readAsDataURL() method to read the specified content
freader.readAsDataURL(file);Finally, a Event processing is equivalent to monitoring the 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); }After the frame loading is completed, the final result is a base64 encoded src address, the specific reason why I will check it out next time and then write a special article about base64 encoding
<!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('src', e.target.result); } } } </script> </body> </html>I believe you have mastered the method after reading the case in this article, please pay attention for more exciting things Other related articles on php Chinese website! Recommended reading:
Set cookie expiration, automatic update and automatic acquisition
Use import and require to package JS
How to deal with the option overlay of select
The above is the detailed content of How to preview images locally when uploading them. For more information, please follow other related articles on the PHP Chinese website!