1//多张图片上传前预览
function previewFiles() { 2 var preview = document.querySelector('#box'); 3 var files = document.querySelector('input[type=file]').files 4 function readAndPreview(file) { 5 6 // Make sure `file.name` matches our extensions criteria 7 if ( /\.(jpe?g|png|gif)$/i.test(file.name) ) { 8 var reader = new FileReader(); 9 10 reader.addEventListener("load", function () { 11 var image = new Image(); 12 image.src = this.result; 13 image.height=85; 14 image.width=85; 15 image.margin=5; 16 image.border= "1px solid #ccc" 17 preview.appendChild( image ); 18 }, false); 19 reader.readAsDataURL(file); 20 } 21 } 22 if (files) { 23 [].forEach.call(files, readAndPreview); 24 } 25 26 }
Solve problems in the most elegant way