<!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>
實現上傳圖片直接預覽,避免提交後在讀取圖片的冗余過程