文件上传的几个示例分享【推荐】(4)

通过iframe.load()方法来获取上传文件后返回到contentWindow.document.body中的信息,并且执行自定义回调函数,把参数传递给自定义方法方便使用者自由的控制体验效果

创建的form表单里面只能使用页面选择文件上传的文件file对象,使用jquery的clone()函数无法获取到选择的文件对象(这是一个悲剧),所以我这个使用append把用户使用的那个file对象直接包含到创建的form中去,然后在创建一个初始化的file元素对象到原始的视图中去代替被移除掉的file元素,代码如(这个还真花了我2个小时的时间尝试):

//清空自定义form多余的file元素 form.html(""); var files = $("input[name='" + defOption.fileEleName + "']"); //复制上传控件对象 var filesClone = files.clone(true); filesClone.insertAfter(files); form.append(files);

使用该插件提交原始表单数据的顺序是:用户点击页面的保存按钮-》通过插件创建的上传文件的form表单,提交上传文件-》上传文件返回成功与否的信息-》收到上传成功新文件名称信息,创建个hidden保存新文件名称到原始form中去-》再真实提交原始form表单的其他数据

下面来看下效果图:

文件上传的几个示例分享【推荐】

示例D的后台代码分为两部分:1.上传文件的Action 2.真实获取表单form参数的Action,代码如:

/// <summary> /// 保存form提交的表单数据 /// </summary> /// <returns></returns> [HttpPost] public JsonResult D() { var response = new MoResponse(); var sbLog = new StringBuilder(string.Empty); try { //访问上传文件地址 var path = @"http://localhost:1010/{0}"; sbLog.AppendFormat("账号:{0}<br/>", Request.Params["userName"]); sbLog.AppendFormat("密码:{0}<br/>", Request.Params["userPwd"]); foreach (var item in Request.Params["hidFileNames"].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)) { sbLog.AppendFormat("文件新名称:{0};下载地址:<a href='https://www.jb51.net/article/{1}' target='_blank'>{0}</a><br/>", item, string.Format(path, item)); } response.Status = 1; } catch (Exception ex) { sbLog.AppendFormat("异常信息:{0}", ex.Message); } finally { response.Data = sbLog.ToString(); } return Json(response); } /// <summary> /// 获取上传文件信息 /// </summary> /// <returns></returns> [HttpPost] public ContentResult D_A() { var response = new MoResponse(); response.Data = "上传失败"; try { Thread.Sleep(1000 * 3); var fileCount = Request.Files.Count; //保存文件地址 var uploadPath = @"D:\D\TTest"; var fileNames = string.Empty; for (int i = 0; i < fileCount; i++) { var file = Request.Files[i]; if (file == null || string.IsNullOrEmpty(file.FileName)) { continue; } var fileName = file.FileName; var fileNewName = DateTime.Now.Ticks + fileName; file.SaveAs(Path.Combine(uploadPath, fileNewName)); fileNames += fileNewName + "|"; } if (!string.IsNullOrEmpty(fileNames)) { response.Status = 1; response.Data = fileNames.TrimEnd('|'); } } catch (Exception ex) { response.Data = ex.Message; } return Content(JsonConvert.SerializeObject(response)); }

好了插件需要讲解的就这么多,不知不觉有只剩我一个人了,该回家了,下面给出整体的代码,插件代码请使用连接获取:

插件下载地址:shenniu.upfile-0.0.1.js

Controller代码:

public class ErrorController : Controller { // // GET: /Error/ public ActionResult Index() { return View(); } [HttpPost] public ActionResult A() { var sbLog = new StringBuilder(string.Empty); var fileCount = Request.Files.Count; //访问上传文件地址 var path = @"http://localhost:1010/{0}"; //保存文件地址 var uploadPath = @"D:\D\TTest"; sbLog.AppendFormat("上传文件目录:{0}<br/>", uploadPath); sbLog.AppendFormat("上传文件量:{0}<br/>", fileCount); for (int i = 0; i < fileCount; i++) { var file = Request.Files[i]; if (file == null || string.IsNullOrEmpty(file.FileName)) { continue; } var fileName = file.FileName; var fileNewName = DateTime.Now.Ticks + fileName; sbLog.AppendFormat("第:{0}个文件名称:{1}新名称:{2}下载地址:<a href='https://www.jb51.net/article/{3}' target='_blank'>{2}</a><br/>", i + 1, fileName, fileNewName, string.Format(path, fileNewName)); file.SaveAs(Path.Combine(uploadPath, fileNewName)); } return Content(sbLog.ToString()); } [HttpPost] public ActionResult B(IEnumerable<HttpPostedFileBase> files) { var sbLog = new StringBuilder(string.Empty); var fileCount = files == null ? 0 : files.Count(); //访问上传文件地址 var path = @"http://localhost:1010/{0}"; //保存文件地址 var uploadPath = @"D:\D\TTest"; sbLog.AppendFormat("上传文件目录:{0}<br/>", uploadPath); sbLog.AppendFormat("上传文件量:{0}<br/>", fileCount); var i = 0; foreach (var file in files) { if (file == null || string.IsNullOrEmpty(file.FileName)) { continue; } var fileName = file.FileName; var fileNewName = DateTime.Now.Ticks + fileName; sbLog.AppendFormat("第:{0}个文件名称:{1}新名称:{2}下载地址:<a href='https://www.jb51.net/article/{3}' target='_blank'>{2}</a><br/>", i + 1, fileName, fileNewName, string.Format(path, fileNewName)); file.SaveAs(Path.Combine(uploadPath, fileNewName)); } return Content(sbLog.ToString()); } [HttpPost] public JsonResult C() { Thread.Sleep(1000 * 5); var response = new MoResponse(); var sbLog = new StringBuilder("开始处理..."); try { sbLog.AppendFormat("账号:{0}<br/>", Request.Params["userName"]); sbLog.AppendFormat("密码:{0}<br/>", Request.Params["userPwd"]); var fileCount = Request.Files.Count; //访问上传文件地址 var path = @"http://localhost:1010/{0}"; //保存文件地址 var uploadPath = @"D:\D\TTest"; sbLog.AppendFormat("上传文件目录:{0}<br/>", uploadPath); sbLog.AppendFormat("上传文件量:{0}<br/>", fileCount); for (int i = 0; i < fileCount; i++) { var file = Request.Files[i]; if (file == null || string.IsNullOrEmpty(file.FileName)) { continue; } var fileName = file.FileName; var fileNewName = DateTime.Now.Ticks + fileName; sbLog.AppendFormat("第:{0}个文件名称:{1}新名称:{2}下载地址:<a href='https://www.jb51.net/article/{3}' target='_blank'>{2}</a><br/>", i + 1, fileName, fileNewName, string.Format(path, fileNewName)); file.SaveAs(Path.Combine(uploadPath, fileNewName)); response.Status = 1; } } catch (Exception ex) { sbLog.AppendFormat("异常信息:{0}", ex.Message); } finally { response.Data = sbLog.ToString(); } return Json(response); } /// <summary> /// 保存form提交的表单数据 /// </summary> /// <returns></returns> [HttpPost] public JsonResult D() { var response = new MoResponse(); var sbLog = new StringBuilder(string.Empty); try { //访问上传文件地址 var path = @"http://localhost:1010/{0}"; sbLog.AppendFormat("账号:{0}<br/>", Request.Params["userName"]); sbLog.AppendFormat("密码:{0}<br/>", Request.Params["userPwd"]); foreach (var item in Request.Params["hidFileNames"].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)) { sbLog.AppendFormat("文件新名称:{0};下载地址:<a href='https://www.jb51.net/article/{1}' target='_blank'>{0}</a><br/>", item, string.Format(path, item)); } response.Status = 1; } catch (Exception ex) { sbLog.AppendFormat("异常信息:{0}", ex.Message); } finally { response.Data = sbLog.ToString(); } return Json(response); } /// <summary> /// 获取上传文件信息 /// </summary> /// <returns></returns> [HttpPost] public ContentResult D_A() { var response = new MoResponse(); response.Data = "上传失败"; try { Thread.Sleep(1000 * 3); var fileCount = Request.Files.Count; //保存文件地址 var uploadPath = @"D:\D\TTest"; var fileNames = string.Empty; for (int i = 0; i < fileCount; i++) { var file = Request.Files[i]; if (file == null || string.IsNullOrEmpty(file.FileName)) { continue; } var fileName = file.FileName; var fileNewName = DateTime.Now.Ticks + fileName; file.SaveAs(Path.Combine(uploadPath, fileNewName)); fileNames += fileNewName + "|"; } if (!string.IsNullOrEmpty(fileNames)) { response.Status = 1; response.Data = fileNames.TrimEnd('|'); } } catch (Exception ex) { response.Data = ex.Message; } return Content(JsonConvert.SerializeObject(response)); } } public class MoResponse { public object Data { get; set; } /// <summary> /// 0:失败 1:成功 /// </summary> public int Status { get; set; } }

View代码:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfzdx.html