Home  >  Article  >  Web Front-end  >  How to preview images locally when uploading them

How to preview images locally when uploading them

php中世界最好的语言
php中世界最好的语言Original
2018-03-17 11:53:441853browse

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

event.target.files

##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 a

constructor

Get an instance object of FileReader

var 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

Complete code




 
 Document

 
      暂无图片   
 
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!

Statement:
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