首页 > 代码库 > formData上传图片

formData上传图片

---------------------formData上传图片---------------------

<form id="imageform">
  <img src="http://www.mamicode.com/img/user_logo_center.png" class="user_logo"/>
  <i class="change_pho"></i>
  <input type="file" name="photo" accept="image/png,image/gif,image/jpg,image/jpeg" id="change_pho_btn">
<form>

$("#change_pho_btn").on("change",function(){
  var formData = http://www.mamicode.com/new FormData($("#imageform" )[0]);

  formData.append("type", "avatar");

  $.ajax({
    url: ‘‘,
    type: ‘POST‘,
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (data) {
      $(‘#imageform .user_logo‘).attr(‘src‘,data.img_url);
    },
    error:function(){

    }
  });
})

----------------------获取客户端图片链接--------------------------------

 ---》必须为文件对象或者BLOB对象(2进制对象)

var img_url=window.URL.createObjectURL($("#upload_pic").get(0).files[0])

$("img").attr("src",img_url)

formData上传图片