首页 > 代码库 > jquery.form.js 实现异步上传
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="http://www.mamicode.com/~/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 实现异步上传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。