jquery.form.js 实现异步上传

Posted 望峰游云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquery.form.js 实现异步上传相关的知识,希望对你有一定的参考价值。

前台:

<form id="formSeacrh" action="/ResumeInfo/uploadFile" method="post" enctype="multipart/form-data">
    <div class="form-horizontal">
        <div class="form-group" style="height:100px;overflow-y:auto">
            <div class="col-sm-8" id="filediv">
                <div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">删除</a></div>
                <div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">删除</a></div>
                <div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">删除</a></div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-8">
                <button type="button" class="btn btn-success" style="float:left" id="addbtn">添加附件</button>
                <button type="submit" id="btnSubmit" style="float:right" class="btn btn-facebook">确定</button>
            </div>
        </div>
    </div>
</form>

 

<script src="~/Scripts/jquery.form.js"></script>
<script>
    $(function () {
        $("#formSeacrh").submit(function () {
            var options = {
                //target: ‘#output‘,          //把服务器返回的内容放入id为output的元素中
                //beforeSubmit: showRequest,  //提交前的回调函数
                //url: url,                 //默认是form的action, 如果申明,则会覆盖
                //clearForm: true,          //成功提交后,清除所有表单元素的值
                //resetForm: true,          //成功提交后,重置所有表单元素的值
                //timeout: 3000,            //限制请求的时间,当请求大于3秒后,跳出请求
                success: function (data) {
                    if (data.message == "") {
                        parent.addDetailToDiv(false, data.files);
                        $(parent.document.getElementsByClassName(‘close‘)).click();
                    } else {
                        $.growl.warning({ message: data.message, title: "提示" });
                    }
                },
                error: function (error) { alert(error); },
                url: ‘/ResumeInfo/uploadFile‘, /*设置post提交到的页面*/
                type: "post", /*设置表单以post方法提交*/
                dataType: "json" /*设置返回值类型为文本*/
            }
            $(this).ajaxSubmit(options);
            return false;
        });

        $(‘body‘).on(‘click‘, ‘#addbtn‘, addFile);
        $(‘body‘).on(‘click‘, ‘.deletelink‘, deleteFile);
    });

    function addFile() {
        $(‘#filediv‘).append(‘<div><input type="file" name="file" style="display: inline;" /><a href="javascript:void(0)" class="deletelink">删除</a></div>‘);
    }

    function deleteFile() {
        $(this).parent().remove();
    }
</script>

 

后台:

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public ActionResult uploadFile(string callback = "") {
            ViewBag.callback = callback;
            return View();
        }

        /// <summary>
        /// 上传文件    提交
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult uploadFile(List<HttpPostedFileBase> list) {
            List<fileClass> fileNames = new List<fileClass>();
            var msg = "";
            if (Request.Files.Count == 0) {
                msg = "请选择文件";
            } else {
                try {
                    for (int i = 0; i < Request.Files.Count; i++) {
                        if (Request.Files[i] != null) {
                            string ext = Path.GetExtension(Request.Files[i].FileName);
                            string filename = Guid.NewGuid().ToString() + ext;
                            string path = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                            string directory = Server.MapPath("~" + path);
                            if (!Directory.Exists(directory)) {
                                Directory.CreateDirectory(directory);
                            }
                            string fileFullName = Path.Combine(directory, filename);
                            Request.Files[i].SaveAs(fileFullName);
                            fileClass f = new fileClass();
                            f.filename = Request.Files[i].FileName;
                            f.path = path + filename;
                            fileNames.Add(f);
                        }
                    }
                } catch (Exception ex) {
                    msg = ex.Message;
                }
            }
            return Json(new { message = msg, files = fileNames });
        }

 

以上是关于jquery.form.js 实现异步上传的主要内容,如果未能解决你的问题,请参考以下文章

使用jquery.form.js实现文件上传及进度条前端代码

jquery.form.js ajax提交上传文件

java 异步上传文件

异步上传文件(jquery.form)+进度条+上传到ftp服务器

使用Jersey上传文件

使用jquery.form.js提交表单上传文件