public ActionResult Upload() { string fileType = ""; //string date = ""; //string filename = ""; //string Lastfilename = ""; var request = this.Request; var filedata = this.Request.Files[0]; var uploadfilename = string.Empty; Stopwatch watch = new Stopwatch(); try { watch.Start(); // 如果没有上传文件 if (filedata == null || string.IsNullOrEmpty(filedata.FileName) || filedata.ContentLength == 0) { return(this.HttpNotFound()); } fileType = this.Request.Form["FileType"]; //date = this.Request.Form["date"]; //string filename = this.Request.Form["filename"]; string filename = filedata.FileName; uploadfilename = filename; //Lastfilename = this.Request.Form["Lastfilename"]; DataTable datatable = ExcelHelper.GetDataTableFromExcel(filedata.InputStream); if (fileType == "Product") { this._unitOfWork.SetAutoDetectChangesEnabled(false); _productService.ImportDataTable(datatable); _unitOfWork.BulkSaveChanges(); this._unitOfWork.SetAutoDetectChangesEnabled(true); //_unitOfWork.SaveChanges(); } if (fileType == "Employee") { this._unitOfWork.SetAutoDetectChangesEnabled(false); this._empService.ImportDataTable(datatable); _unitOfWork.BulkSaveChanges(); this._unitOfWork.SetAutoDetectChangesEnabled(true); //_unitOfWork.SaveChanges(); } if (fileType == "CodeItem") { this._unitOfWork.SetAutoDetectChangesEnabled(false); _codeService.ImportDataTable(datatable); _unitOfWork.BulkSaveChanges(); //_unitOfWork.SaveChanges(); this._unitOfWork.SetAutoDetectChangesEnabled(true); } //if (fileType == "Product") //{ // _iBOMComponentService.ImportDataTable(datatable); // _unitOfWork.SaveChanges(); //} uploadfilename = System.IO.Path.GetFileName(filedata.FileName); string folder = Server.MapPath("~/UploadFiles"); string time = DateTime.Now.ToString().Replace("\\", "").Replace("/", "").Replace(".", "").Replace(":", "").Replace("-", "").Replace(" ", ""); //获取时间 string newFileName = string.Format("{0}_{1}", time, uploadfilename); //重组成新的文件名 if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } else { //string LastFile = Server.MapPath(Lastfilename); //FileInfo nmmFile = new FileInfo(LastFile); //if (nmmFile.Exists) //{ // nmmFile.Delete(); //} } string virtualPath = string.Format("{0}\\{1}", folder, newFileName); // 文件系统不能使用虚拟路径 //string path = this.Server.MapPath(virtualPath); filedata.SaveAs(virtualPath); watch.Stop(); //获取当前实例测量得出的总运行时间(以毫秒为单位) var elapsedTime = watch.ElapsedMilliseconds.ToString(); return(Json(new { success = true, filename = "/UploadFiles/" + newFileName, elapsedTime = elapsedTime }, JsonRequestBehavior.AllowGet)); } catch (System.Data.SqlClient.SqlException e) { Logger.Error(uploadfilename, "FileUpload", e.InnerException.InnerException.Message, fileType, e.Source, e.StackTrace); return(Json(new { success = false, message = e.InnerException.InnerException.Message }, JsonRequestBehavior.AllowGet)); } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { Logger.Error(uploadfilename, "FileUpload", e.InnerException.InnerException.Message, fileType, e.Source, e.StackTrace); return(Json(new { success = false, message = e.InnerException.InnerException.Message }, JsonRequestBehavior.AllowGet)); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { var errormessage = string.Join(",", e.EntityValidationErrors.Select(x => x.ValidationErrors.FirstOrDefault()?.PropertyName + ":" + x.ValidationErrors.FirstOrDefault()?.ErrorMessage)); Logger.Error(uploadfilename, "FileUpload", errormessage, fileType, e.Source, e.StackTrace); return(Json(new { success = false, message = errormessage }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { Logger.Error(uploadfilename, "FileUpload", e.Message, fileType, e.Source, e.StackTrace); return(Json(new { success = false, message = e.Message }, JsonRequestBehavior.AllowGet)); } }