VVL1295

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

jQuery 文件上传

  直接上代码:

 <script>
   //form Submit action
   $("form").submit(function(evt){     
   evt.preventDefault();

   var formData = new FormData($(this)[0]); 

   $.ajax({
     url: 'fileUpload',
     type: 'POST',
     data: formData,
     async: false,
     cache: false,
     contentType: false,
     enctype: 'multipart/form-data',
     processData: false,
     success: function (response) {
       alert(response);
     }
   });
   return false;
 });
</script>

  其实这样即可:

var fd = new FormData(document.querySelector("form"));
fd.append("CustomField", "This is some extra data");
$.ajax({
  url: "stash.php",
  type: "POST",
  data: fd,
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
});

 

posted on 2016-11-23 14:15  bobo2018  阅读(96)  评论(0)    收藏  举报