图片预览

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;">');
  }
});

 

posted on 2019-08-28 15:03  *孤独的夜行者*  阅读(235)  评论(0编辑  收藏  举报

导航