/// <summary> /// 保存附件 /// </summary> /// <param name="id"></param> private void SaveAttach(Guid id) { try { HttpFileCollection files = HttpContext.Current.Request.Files; if (files.Count == 0) { NewsContent n = NewsContent.Load(id); n.Annex = 2;//没有附件 n.Save(); } else { for (int i = 0; i < files.Count; i++) { HttpPostedFile postedFile = files[i]; if (postedFile.ContentLength > 20971520) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('上传附件不能超过20M,附件:" + postedFile.FileName + "上传失败');</script>"); return; } else { string fileSHowName = postedFile.FileName; int dotIndex = postedFile.FileName.IndexOf("\\"); string fileName = string.Empty; if (dotIndex == -1) { fileName = CY.Utility.Common.FileUtility.GetFileName(postedFile.FileName);//文件名 } else { string fileNameTemp = CY.Utility.Common.FileUtility.GetFileName(postedFile.FileName); int indElx = fileNameTemp.LastIndexOf("\\") + 1; fileName = fileNameTemp.Substring(indElx); } //string fileName = CY.Utility.Common.FileUtility.GetFileName(postedFile.FileName);//文件名 string fileExtension = CY.Utility.Common.FileUtility.GetFileExtension(postedFile.FileName);//后缀名 string appPath = CY.Utility.Common.RequestUtility.CurPhysicalApplicationPath;//绝对路径 string dirPath = string.Empty; string httpPath = string.Empty; string annexTypeComon = string.Empty; if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".gif" || fileExtension == ".png" || fileExtension == ".bmp") { dirPath = appPath + CY.Utility.Common.AnnexUtility.NewsAnnexImgDir; annexTypeComon = CY.Utility.Common.AnnexUtility.ImgAnnexType; httpPath = "/Content/News/Img/"; } else if (fileExtension == ".doc" || fileExtension == ".docx" || fileExtension == ".xls" || fileExtension == ".pdf" || fileExtension == ".txt") { dirPath = appPath + CY.Utility.Common.AnnexUtility.NewsAnnexOfficeDir; annexTypeComon = CY.Utility.Common.AnnexUtility.DocAnnexType; httpPath = "/Content/News/Office/"; } else if (fileExtension == ".rar" || fileExtension == ".zip") { dirPath = appPath + CY.Utility.Common.AnnexUtility.NewsAnnexRARDir; annexTypeComon = CY.Utility.Common.AnnexUtility.RaRAnneType; httpPath = "/Content/News/RAR/"; } else { dirPath = appPath + CY.Utility.Common.AnnexUtility.NewsAnnexOtherDir; annexTypeComon = CY.Utility.Common.AnnexUtility.OtherAnneType; httpPath = "/Content/News/Other/"; } System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dirPath); if (!di.Exists) { di.Create(); } string FileName = fileName + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString(); postedFile.SaveAs(dirPath + "\\" + FileName + fileExtension); AnnexType annexTYPE = AnnexType.SelectAnnexTypeByTypeName(annexTypeComon); Guid typeID = new Guid(); if (annexTYPE == null) { AnnexType annexType = new AnnexType(); annexType.TypeName = annexTypeComon; annexType.Save(); typeID = annexType.Id; } else { typeID = annexTYPE.Id; } Annex annex = new Annex(); annex.AnnexTypeID = typeID; annex.ContentType = 1; annex.ContentID = id; annex.Name = fileName; annex.AnnexURI = httpPath + FileName + fileExtension; annex.Save(); } } } } catch (Exception ex) { lbErr.Text = ex.Message; } }
private void SaveFiles(Guid ContentID) { HttpFileCollection files = HttpContext.Current.Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFile postedFile = files[i]; if (postedFile.ContentLength > 20971520) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('上传附件不能超过20M,附件:" + postedFile.FileName + "上传失败');</script>"); return; } else { string fileSHowName = postedFile.FileName; int dotIndex = postedFile.FileName.IndexOf("\\"); string fileName = string.Empty; if (dotIndex == -1) { fileName = CY.Utility.Common.FileUtility.GetFileName(postedFile.FileName);//文件名 } else { string fileNameTemp = CY.Utility.Common.FileUtility.GetFileName(postedFile.FileName); int indElx = fileNameTemp.LastIndexOf("\\") + 1; fileName = fileNameTemp.Substring(indElx); } string fileExtension = CY.Utility.Common.FileUtility.GetFileExtension(postedFile.FileName);//后缀名 string appPath = CY.Utility.Common.RequestUtility.CurPhysicalApplicationPath;//绝对路径 string dirPath = string.Empty; string httpPath = string.Empty; string annexTypeComon = string.Empty; if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".gif" || fileExtension == ".png" || fileExtension == ".bmp") { dirPath = appPath + CY.Utility.Common.AnnexUtility.EmailAnnexImgDir; annexTypeComon = CY.Utility.Common.AnnexUtility.ImgAnnexType; httpPath = "../../Content/Email/Img/"; } else if (fileExtension == ".doc" || fileExtension == ".docx" || fileExtension == ".xls" || fileExtension == ".pdf" || fileExtension == ".txt") { dirPath = appPath + CY.Utility.Common.AnnexUtility.EmailAnnexDorDir; annexTypeComon = CY.Utility.Common.AnnexUtility.DocAnnexType; httpPath = "../../Content/Email/Doc/"; } else if (fileExtension == ".rar" || fileExtension == ".zip") { dirPath = appPath + CY.Utility.Common.AnnexUtility.EmailAnnexRARDir; annexTypeComon = CY.Utility.Common.AnnexUtility.RaRAnneType; httpPath = "../../Content/Email/RAR/"; } else { dirPath = appPath + CY.Utility.Common.AnnexUtility.EmailAnnexOtherDir; annexTypeComon = CY.Utility.Common.AnnexUtility.OtherAnneType; httpPath = "../../Content/Email/Other/"; } System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dirPath); if (!di.Exists) { di.Create(); } string FileName = fileName + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString(); postedFile.SaveAs(dirPath + "\\" + FileName + fileExtension);//保存文件 Guid typeID=new Guid(); AnnexType annexType = AnnexType.SelectAnnexTypeByTypeName(annexTypeComon); if (annexType == null) { AnnexType annexTypeNew = new AnnexType(); annexTypeNew.TypeName = annexTypeComon; annexTypeNew.Save(); typeID=annexTypeNew.Id; } else { typeID=annexType.Id; } Annex annexSave = new Annex(); annexSave.ContentID = ContentID; annexSave.AnnexTypeID = typeID; annexSave.ContentType = 8;//站内信 annexSave.Name = fileName; annexSave.AnnexURI = httpPath + FileName + fileExtension; annexSave.Save(); } } }