public ActionResult Upload(HttpPostedFileBase Filedata) { string modelType = ""; //string date = ""; //string filename = ""; //string Lastfilename = ""; try { // 如果没有上传文件 if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0) { return(this.HttpNotFound()); } modelType = this.Request.Form["modelType"]; //date = this.Request.Form["date"]; //filename = this.Request.Form["filename"]; //Lastfilename = this.Request.Form["Lastfilename"]; DataTable datatable = ExcelHelper.GetDataTableFromExcel(Filedata.InputStream); if (modelType == "SKU") { _sKUService.ImportDataTable(datatable); _unitOfWork.SaveChanges(); } if (modelType == "BOMComponent") { _iBOMComponentService.ImportDataTable(datatable); _unitOfWork.SaveChanges(); } if (modelType == "ProcessStep") { _setpservice.ImportDataTable(datatable); _unitOfWork.SaveChanges(); } if (modelType == "Station") { _stationservice.ImportDataTable(datatable); _unitOfWork.SaveChanges(); } string 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); return(Json(new { success = "true", filename = "/UploadFiles/" + newFileName }, JsonRequestBehavior.AllowGet)); } catch (System.Data.SqlClient.SqlException e) { return(Json(new { success = "false", message = e.InnerException.InnerException.Message }, JsonRequestBehavior.AllowGet)); } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { return(Json(new { success = "false", message = e.InnerException.InnerException.Message }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { return(Json(new { success = "false", message = e.Message }, JsonRequestBehavior.AllowGet)); } }