ajax无刷新上传文件MVC

@{
    ViewBag.Title = " Assistant";
    //Layout = null;
}
<script type="text/javascript">

        $(document).ready(function () {
            $("#fileButton").click(function () {
                var files = $("#fileInput").get(0).files;
                var fileData = new FormData();

                for (var i = 0; i < files.length; i++) {
                    fileData.append("fileInput",files[i]);
                }

                $.ajax({
                    type: "POST",url: "/Document/UploadFiles",dataType: "json",contentType: false,// Not to set any content header
                    processData: false,// Not to process data
                    data: fileData,success: function (result,status,xhr) {
                        alert(result);
                        $("#fileInput").val("");
                    },error: function (xhr,error) {
                        alert(status);
                    }
                });
            });

            $(document).ajaxStart(function () {
                //$("#loadingImg").show();
               // $("#fileButton").prop('disabled',true);
            });

            $(document).ajaxStop(function () {
                //$("#loadingImg").hide();
               // $("#fileButton").prop('disabled',false);
               // $("#fileInput").val("");
            });

        });

</script>

<!--Attachment 5-->
<div class="content">


                    <textarea id="txtID1" cols="60" rows="3" style="color:black"></textarea>

                        <div class="row">
                            <input type="file" id="fileInput" multiple />
                            <input type="button" id="fileButton" value="Upload Files" /><br />
                        </div>
</div>
 
 

controller层

[HttpPost]
        public ActionResult UploadFiles()
        {
            string path = Server.MapPath("~/WorkFolder/Upload/Inspection/");
            HttpFileCollectionBase files = Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFileBase file = files[i];
                file.SaveAs(path + file.FileName);
            }
            return Json(files.Count + " Files Uploaded!");
        }

相关文章

$.AJAX()方法中的PROCESSDATA参数 在使用jQuery的$.ajax()方...
form表单提交的几种方式 表单提交方式一:直接利用form表单提...
文章浏览阅读1.3k次。AJAX的无刷新机制使得在注册系统中对于...
文章浏览阅读1.2k次。 本文将解释如何使用AJAX和JSON分析器在...
文章浏览阅读2.2k次。/************************** 创建XML...
文章浏览阅读3.7k次。在ajax应用中,通常一个页面要同时发送...