public string DocUpload(HttpPostedFileBase Filedata) { try { Guid billGuid = Guid.Empty; if (Request.QueryString["fid"] != null) Guid.TryParse(Request.QueryString["fid"], out billGuid); // 如果没有上传文件 if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0) { return "未找到上传文件!"; } // 保存到 ~/photos 文件夹中,名称不变 string filename = System.IO.Path.GetFileName(Filedata.FileName); string virtualPath = string.Format("~/FileUpload/Doc/{0}", filename); // 文件系统不能使用虚拟路径 string path = this.Server.MapPath(virtualPath); Filedata.SaveAs(path); Expression<Func<OrderDoc, bool>> where = PredicateExtensionses.True<OrderDoc>(); where = where.And(m => m.RESOURCE_ID == billGuid); IEnumerable<OrderDoc> list = unitOfWork.OrderDocBLL.GetEntitys(where); List<OrderDoc> docList = list.ToList<OrderDoc>(); OrderDoc orderDoc = new OrderDoc(); if (docList.Count == 1) { orderDoc = docList[0]; orderDoc.FileContent = GetFileBytes(path); orderDoc.FileLength = Filedata.ContentLength; orderDoc.FileName = filename; orderDoc.FilePath = path; orderDoc.FileType = ".doc"; orderDoc.RESOURCE_ID = billGuid; unitOfWork.OrderDocBLL.UpdateEntity(orderDoc); unitOfWork.Save(); } else { orderDoc.GUID = Guid.NewGuid(); orderDoc.FileContent = GetFileBytes(path); orderDoc.FileLength = Filedata.ContentLength; orderDoc.FileName = filename; orderDoc.FilePath = path; orderDoc.FileType = ".doc"; orderDoc.RESOURCE_ID = billGuid; unitOfWork.OrderDocBLL.InsertEntity(orderDoc); unitOfWork.Save(); } return orderDoc.GUID + ":" + orderDoc.FileName; } catch (Exception ex) { Log.Error(ex.Message); return string.Empty; } }
public ActionResult CreateTextDoc() { Guid billGuid = Guid.Empty; Guid.TryParse(Request.QueryString["fid"], out billGuid); var entity = unitOfWork.OfficialDocumentBLL.GetEntityByID(billGuid); var dir = AppDomain.CurrentDomain.BaseDirectory; var path = dir + "\\WordTemplate\\发文模版.doc"; try { Document doc = new Document(path); if (doc != null) { #region 获取文章数据 DataTable dtNew = new DataTable(); dtNew.Columns.Add("份号"); dtNew.Columns.Add("保密信息"); dtNew.Columns.Add("紧急程度"); dtNew.Columns.Add("发文机关全称"); dtNew.Columns.Add("部门代字"); dtNew.Columns.Add("年份"); dtNew.Columns.Add("发文顺序号"); dtNew.Columns.Add("签发人姓名"); dtNew.Columns.Add("正文标题"); dtNew.Columns.Add("主送机关名称"); dtNew.Columns.Add("正文"); dtNew.Columns.Add("发文机关署名"); dtNew.Columns.Add("成文日期"); dtNew.Columns.Add("抄送机关名称"); dtNew.Columns.Add("印发机关"); dtNew.Columns.Add("印发日期"); DataRow newRow = dtNew.NewRow(); newRow["份号"] = entity.CopyNumber; if (string.IsNullOrEmpty(entity.Secrecylevel) && string.IsNullOrEmpty(entity.SecrecyDeadline)) newRow["保密信息"] = ""; else newRow["保密信息"] = entity.Secrecylevel + "★" + entity.SecrecyDeadline; newRow["紧急程度"] = entity.Urgencylevel; newRow["发文机关全称"] = entity.DeptFlag; newRow["部门代字"] = entity.CopyDept; newRow["年份"] = entity.WordYear; newRow["发文顺序号"] = entity.WordNumber; newRow["签发人姓名"] = entity.IssuerName; newRow["正文标题"] = entity.Title; newRow["主送机关名称"] = entity.MainDept; newRow["正文"] = entity.MainBody; newRow["发文机关署名"] = entity.DeptSign; if (entity.WriteDate != null && entity.WriteDate > DateTime.Parse("1573-1-1")) newRow["成文日期"] = DateTime.Parse(entity.WriteDate.ToString()).ToString("yyyy年MM月dd日"); newRow["抄送机关名称"] = entity.CopyDept; newRow["印发机关"] = entity.PrintDept; if (entity.PrintDate != null && entity.PrintDate > DateTime.Parse("1573-1-1")) newRow["印发日期"] = DateTime.Parse(entity.PrintDate.ToString()).ToString("yyyy年MM月dd日"); #endregion if (newRow != null) doc.MailMerge.Execute(newRow); } #region 保存填充后的文件、下载 string newPath = dir + "\\FileUpload\\Doc\\" + entity.Title + ".doc"; if (System.IO.File.Exists(newPath)) { System.IO.File.Delete(newPath); } doc.Save(newPath); byte[] fileContent = System.IO.File.ReadAllBytes(newPath); #region 保存正文文件 OrderDoc orderDoc = new OrderDoc(); orderDoc.FileContent = fileContent; orderDoc.FileLength = fileContent.Length; orderDoc.FileName = entity.Title + ".doc"; orderDoc.FilePath = newPath; orderDoc.FileType = ".doc"; orderDoc.GUID = Guid.NewGuid(); orderDoc.RESOURCE_ID = entity.GUID; unitOfWork.OrderDocBLL.InsertEntity(orderDoc); unitOfWork.Save(); #endregion Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(entity.Title + ".doc", Encoding.UTF8))); Response.ContentEncoding = Encoding.GetEncoding("gb2312"); Response.ContentType = "application/octet-stream"; //res.ContentType = "application/msword"; Response.Clear(); Response.BinaryWrite(fileContent); Response.Flush(); #endregion } catch (Exception ex) { LogWritter.WriteSystemExceptionLog(ex); } return new EmptyResult(); }