public SaveFile ( string fileName ) : void | ||
fileName | string | |
Результат | void |
public ActionResult UploadFile(BlogModel bModel) { var testSample = new BlogSample() { UserName = "******", Id = Guid.NewGuid().ToString("N") }; if (ModelState.IsValid) { var fileName = bModel.BlogPhoto.FileName; var success = UploadManager.SaveFile(bModel.BlogPhoto.InputStream, fileName, testSample.UserName, testSample.Id); if (!success) { // TODO(your code) } //var filePath = Server.MapPath(string.Format("~/{0}", "File")); //bModel.BlogPhoto.SaveAs(Path.Combine(filePath, fileName)); ModelState.Clear(); } return(View()); }
/// <summary> /// 上传文件的具体方法 /// </summary> /// <returns>成功则为true,失败异常则为False</returns> public JsonResult UploadFile(FileViewModel fileModel) { string msg = "true"; if (fileModel.DesignId == 0) { msg = "设计案ID获取出错!"; return(Json(msg)); } //if (Session["method"].ToString() == "Y") //{ // string str = string.Format("<script>alert('重复操作!');parent.location.href='TrackLogIndex';</script>"); // return Json(str); //} SetEmployee(); fileModel.FileName = fileModel.UploadStream.FileName; fileModel.UserName = employeeName; fileModel.StoreName = storeBLL.GetModel(p => p.ID == storeID).称; var m = fileModel.UploadStream.InputStream; msg = IsAllowedExtension(fileModel.UploadStream, fileModel.Filetype); if (msg != "true") { return(Json(msg)); //如果上传文件不匹配,则返回出错信息! } var path = UploadManager.SaveFile(fileModel.UploadStream, fileModel.FileName, fileModel.StoreName, fileModel.DesignId.ToString(), fileModel.Filetype.ToString()); //获得上传路径 fileModel.Path = path; if (path == null) /*|| design_ProjectDrawingsBLL.GetModel(p => p.存储路径 == path) != null)*/ { return(Json("false")); } 销售_设计案提交表_项目相关图纸 model = new 销售_设计案提交表_项目相关图纸 { 提交人 = fileModel.UserName, 提交时间 = DateTime.Now, 更新人 = fileModel.UserName, 更新日期 = DateTime.Now, 设计案提交表ID = fileModel.DesignId, 文件名 = fileModel.FileName }; model.设计案提交表ID = fileModel.DesignId; var s = ""; switch (fileModel.Filetype) { case FileType.CAD图: s = fileModel.Filetype.ToString(); model.存储路径 = fileModel.Path; model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.效果3D图: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.对比图: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.PDF文件: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.PPT文件: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.Excel文件: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.其它: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; case FileType.完成效果文件: model.存储路径 = fileModel.Path; s = fileModel.Filetype.ToString(); model.文件类型 = fileTypeBLL.GetModel(p => p.文件类型 == s).ID; break; default: break; } if (ModelState.IsValid) { design_ProjectDrawingsBLL.Add(model); Session["method"] = "Y"; ModelState.Clear(); } else { List <string> sb = new List <string>(); //获取所有错误的Key List <string> Keys = ModelState.Keys.ToList(); //获取每一个key对应的ModelStateDictionary foreach (var key in Keys) { var errors = ModelState[key].Errors.ToList(); //将错误描述添加到sb中 foreach (var error in errors) { sb.Add(error.ErrorMessage); } } msg = "上传文件出错:"; foreach (var item in sb) { msg += item.ToString() + "<br/>"; } return(Json(msg)); } msg = "上传成功!"; return(Json(msg)); }