FormData图片预览上传

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src="js/jquery.min.js"></script>
<style>
  #preview{
    width:200px;
    height:200px;
    border:1px solid #ccc;
  }
</style>
 </head>
 <body>

    <input id="uploadImageBtn" name="imageList" type="file" onchange="showImage(this)" style="display: none">
    <div id="imagewrap" class="imagewrap">
      <img src="">
    </div>
    <input class="modify" type="button" value="上传图片" onclick="popSelectImageBox()">
    <input type="button" id="tijiao" value="提交图片">            

<script>
/**
 * 弹出图像选择框
 */
function popSelectImageBox(){
    $("#uploadImageBtn").trigger("click");
}
/**
 * 显示图片预览
 * 上传按钮
 */
function showImage(that){
  var objUrl = getObjectURL(that.files[0]) ;
  if (objUrl) {
     $("#imagewrap>img").attr("src", objUrl);
  }
}
/**
 * 获取上传图片的url
 *file 上传的文件对象
 */
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
       return url;
}
/**
*上传图片到后端
*/
$("#tijiao").on("click",function(){
  var uploadImageBtn=document.getElementById("uploadImageBtn");
  var data=new FormData();
  data.append("newflies",uploadImageBtn.files[0]);
  console.dir(data.get("newflies"));
});
  </script>
 </body>
</html>

结果:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值