本demo基于bootstrap-prettyfile.js
html代码如下:
1 <div id="file-pretty"> 2 <form id="ui-form-file-upload" name="form-file-upload" method="post" target="ui-iframe-file-upload" action="/Env/SoilSampleCheck/FileSubmit" enctype="multipart/form-data"> 3 <input type="file" name="fileName" class="form-control" style="display:none;"> 4 <input type="hidden" name="projectId" id="hidProjectId" /> 5 </form> 6 <iframe name="ui-iframe-file-upload" id="ui-iframe-file-upload" style="position:absolute; top:-9999px; left:-9999px;"></iframe> 7 </div>
JavaScript代码如下:
1 //文件上传 2 $(‘#file-pretty input[type="file"]‘).prettyFile().change(function () { 3 4 var projectId = $("#hidProjectId").val(); 5 if (projectId == "") { 6 tincher.message("请先选择项目", "error"); 7 } else { 8 9 var param = { projectId: projectId }; 10 param = $.forgeryToken(param); 11 tincher.execute("/Env/SoilSampleCheck/CheckSoilItem1", param, function (result, response) { 12 if (result) { 13 $("#ui-form-file-upload").submit(); 14 } else { 15 tincher.message(response.message, "error"); 16 } 17 }); 18 } 19 }); 20 21 $("#ui-iframe-file-upload").on(‘load‘, function () { 22 var response = ""; 23 if (this.contentWindow) { 24 response = this.contentWindow.document.body ? this.contentWindow.document.body.innerText : null; 25 26 } else if (this.contentDocument) { 27 response = this.contentDocument.document.body ? this.contentDocument.document.body.innerText : null; 28 } 29 if (response != "") { 30 //有返回值 31 var result = JSON.parse(response) || {}; 32 tincher.message(result.message); 33 if (result.state == "success") { 34 $("#hidFilePath").val(result.context.url); 35 excelShow(); 36 } 37 } 38 })
C#代码如下:
1 /// <summary> 2 /// 单个文件上传 3 /// </summary> 4 /// <returns></returns> 5 [HttpPost] 6 public ActionResult FileSubmit(FormCollection form) 7 { 8 //文件 9 var postFiles = Request.Files; 10 //没有文件 11 if (postFiles == null && postFiles.Count == 0) 12 { 13 return Error("请选择要上传的附件"); 14 } 15 try 16 { 17 var postFile = postFiles[0]; 18 Thread.Sleep(200);////延迟200毫秒 19 //文件保存目录路径 20 string savePath = "/Content/attached/"; 21 //模块名 22 var module = "SoilSampleCheckModule"; 23 var projectId = form["projectId"]; 24 //真实路径 25 var realPath = Server.MapPath(savePath); 26 //创建文件夹-模块文件夹 27 realPath += module + "\\"; 28 savePath += module + "/"; 29 if (!Directory.Exists(realPath)) 30 { 31 Directory.CreateDirectory(realPath); 32 } 33 //创建文件夹-日期文件夹 34 string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); 35 realPath += ymd + "\\"; 36 savePath += ymd + "/"; 37 if (!Directory.Exists(realPath)) 38 { 39 Directory.CreateDirectory(realPath); 40 } 41 //扩展名 42 string fileExt = Path.GetExtension(postFile.FileName); 43 //文件名 44 var fullFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; 45 //保存文件 46 postFile.SaveAs(realPath + fullFileName); 47 //返回保存的url 48 savePath += fullFileName; 49 return Success("上传成功!", new { name = postFile.FileName, url = savePath }); 50 } 51 catch (Exception ex) 52 { 53 return Error(ex.Message); 54 } 55 }