jquery上传头像

19 篇文章 0 订阅
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jquery上传头像</title>
<script src="js/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<img id="pic" style="width:100px;height:100px;border-radius:50%;" src="http://www.jq22.com/tx/28.png">
<input id="upload" name="file" accept="image/*" type="file" style="display: none">
<!-- <ul>
        <li style="width:23%;float:left;margin:10px;height:200px;background:#eee;"><img style="height:200px;width:100%;" src="/upload/thumbs/efb3fb08b062a55e28a0e15ad2228514.jpg" alt=""></li>
 </ul> -->

<script>
  $(function() {
      $("#pic").click(function() {
          $("#upload").click(); //隐藏了input:file样式后,点击头像就可以本地上传
          $("#upload").on("change", function() {
              var objUrl = getObjectURL(this.files[0]); //获取图片的路径,该路径不是图片在本地的路径
              console.log(this.files)
              if (objUrl) {
                  $("#pic").attr("src", objUrl); //将图片路径存入src中,显示出图片
                  upimg();
              }
          });
      });
  });

  //建立一?可存取到?file的url
  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;
  }
  //上传头像到服务器
  function upimg() {
      console.log(344)
      var pic = $('#upload')[0].files[0];
      console.log(pic)
      var file = new FormData();
      file.append('image', pic);
      file['image']=pic
      console.log(file)
      // $.ajax({
      //     url: "/uploadImg",
      //     type: "post",
      //     data: file,
      //     cache: false,
      //     contentType: false,
      //     processData: false,
      //     success: function(data) {
      //         console.log(data);
      //         var res = data;
      //         $("#resimg").append("<img src='/" + res + "'>")
      //     }
      // });
  }
</script>

</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值