图片预览

html:

<div>
  附件:
  <input class="pic-add" type="file" name='file' accept="image/*">
  <br><br>
  <div class="imgDiv"></div>
</div>

jquery:

function getObjectURL(file){
  var url = '';
  if(window.createObjectURL !== undefined){
    url = window.createObjectURL(file);
  } else if (window.URL !== undefined){
    url = window.URL.createObjectURL(file);
  } else if(window.webitURL !== undefined){
    url = window.webitURL.createObjectURL(file);
  }
  return url;
}
$(".pic-add").on("change",function(){
  var objUrl = getObjectURL(this.files[0]);
  if(objUrl){
    $('.imgDiv').append('<img src="'+ objUrl +'" width="70px" height="70px" style="margin-left:2px;">');
  }
});

原文地址:https://www.cnblogs.com/jhy-ocean/p/11424135.html