ajax无刷新上传文件MVC

前端之家收集整理的这篇文章主要介绍了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相关文章