public static bool SaveFile(HttpPostedFileBase file, string path, int officeId) { using (MainDataContext dbContext = new MainDataContext()) { TOfficeAttachment entity = new TOfficeAttachment(); var list = dbContext.TOfficeAttachment.Select(t => t.编码); if (list.LongCount() > 0) { entity.编码 = list.Max() + 1; } else { entity.编码 = 1; } int lastIndex = file.FileName.LastIndexOf("\\"); entity.原附件名 = file.FileName.Substring(lastIndex + 1, file.FileName.Length - lastIndex - 1); //文件名 entity.编码附件名 = System.Guid.NewGuid().ToString() + entity.原附件名; //编码附件名 entity.文件大小 = (float)(file.ContentLength * 1.0 / 1024); //文件大小 entity.附件路径 = path + entity.编码附件名; entity.办公编码 = officeId; file.SaveAs(entity.附件路径); //上传至服务器 dbContext.TOfficeAttachment.InsertOnSubmit(entity); dbContext.SubmitChanges(); return(true); } }
/// <summary> /// 下载附件 /// </summary> /// <param name="fileId">附件编码</param> /// <returns></returns> public ActionResult FileDownLoad(int fileId) { BLL.Office.Office office = new BLL.Office.Office(); TOfficeAttachment cot = office.DownLoadFile(fileId); string path = AppConfig.GetUpload() + cot.编码附件名; return(new DownloadResult { VirtualPath = path, FileDownloadName = cot.原附件名 }); }