Input file upload image preview is actually very simple, but it feels amazing if you have never done it before. Today I will take off its mysterious veil. In fact, the principle is really simple. Everyone can understand it through a piece of code below.
The specific code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="jquery.js"></script> </head> <body> <input type="file" multiple id="inputs"/> //multiple(多文件上传) <div id='dd'></div> <script> $(document).ready(function () { $("#inputs").change(function () { var fil = this.files; for (var i = 0; i < fil.length; i++) { reads(fil[i]); } }); }); function reads(fil){ var reader = new FileReader(); reader.readAsDataURL(fil); reader.onload = function() { document.getElementById("dd").innerHTML += "<img src='"+reader.result+"'>"; }; } </script> </body> <html>
Achieve direct preview of uploaded images, avoiding the redundant process of reading images after submission