HTML部分

<form id="uploadForm" enctype="multipart/form-data"><input id="selectFile" type="file" name="file"/></form>
<button type="button" id="upload">确认上传</button>

Js部分

$(document).ready(function(){
  $("#upload").click(function () {
    if($('#selectFile').val().length == 0){
      alert("请选择文件");
      return;
    }
    var formData = new FormData($('#uploadForm')[0]);
    $.ajax({
      type: 'post',
      url: "/",
      data: formData,
      cache: false,
      processData: false,
      contentType: false,
    }).success(function (data) {
      alert("上传成功");
    }).error(function () {
      alert("上传失败");
    });
  });
});

标签: none

添加新评论