/// <summary> /// 添加附件到数据库 /// </summary> /// <param name="model"></param> /// <returns></returns> public int AddFile(Attachment model) { var file = db.Attachments.Add(model); db.SaveChanges(); int row = 0; if (file != null) { row = file.Id; } return row; }
/// <summary> /// 上传文件,返回存储文件的Id /// </summary> /// <returns></returns> public JsonResult UploadFile() { string result = ""; int row = 0; foreach (string upload in Request.Files) { if (!HasFiles.HasFile(Request.Files[upload])) { continue; } string miniType = Request.Files[upload].ContentType; string fileType = Path.GetExtension(Request.Files[upload].FileName); Stream fileStream = Request.Files[upload].InputStream; string oldFilename = Path.GetFileName(Request.Files[upload].FileName); DateTime nowTime = DateTime.Now; Random rd = new Random(); string newFileName = nowTime.Year.ToString() + nowTime.Month.ToString() + nowTime.Day.ToString() + nowTime.Hour.ToString() + nowTime.Minute.ToString() + nowTime.Second.ToString() + rd.Next(1000, 1000000) + ".jpg"; string rootPath = HttpContext.Server.MapPath("../../UpLoadFile/"); string temp = @"../../UpLoadFile/"; int size = Request.Files[upload].ContentLength; if (!Directory.Exists(rootPath)) { Directory.CreateDirectory(rootPath); } Request.Files[upload].SaveAs(rootPath + newFileName); var file = new Attachment() { FileType = fileType, FileSzie = size, OldFileName = newFileName, NewFileName = oldFilename, FilePath = temp, AddTime = nowTime }; row = repository.AddFile(file); result += row + ","; } return Json(row); }