using System; using System.Collections; using System.Collections.Generic; using System.Data.Linq; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using BLL; namespace BLL { /// /// 上传附件相关 /// public class UploadAttachmentService { #region 附件显示 /// /// 附件显示 /// /// 文件夹路径 /// 附件路径 /// 附件显示HTML public static string ShowAttachment(string rootValue, string path) { string htmlStr = string.Empty; if (!string.IsNullOrEmpty(path)) { htmlStr = ""; string[] arrStr = path.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arrStr.Length; i++) { if (!string.IsNullOrEmpty(arrStr[i])) { string[] urlArray = arrStr[i].Split('\\'); string scanUrl = string.Empty; for (int j = 0; j < urlArray.Length; j++) { scanUrl += urlArray[j] + "|"; } string url = rootValue + arrStr[i].Replace('\\', '/'); string[] subUrl = url.Split('/'); string fileName = subUrl[subUrl.Count() - 1]; string newFileName = fileName.Substring(fileName.IndexOf("~") + 1); if (newFileName.Contains("_")) { if (newFileName.Substring(0, 1) == "_") { newFileName = newFileName.Substring(1, newFileName.Length - 1); } else { newFileName = newFileName.Replace(newFileName.Split('_')[0], ""); newFileName = newFileName.Substring(1); } } htmlStr += ""; } } htmlStr += "
" + newFileName + "
"; } return htmlStr; } #endregion #region 附件显示 带删除 /// /// 附件显示 /// /// 文件夹路径 /// 附件路径 /// 附件显示HTML public static string ShowAndDeleteAttachment(string rootValue, string path) { string htmlStr = string.Empty; if (!string.IsNullOrEmpty(path)) { htmlStr = ""; string[] arrStr = path.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arrStr.Length; i++) { if (!string.IsNullOrEmpty(arrStr[i])) { string[] urlArray = arrStr[i].Split('\\'); string scanUrl = string.Empty; for (int j = 0; j < urlArray.Length; j++) { scanUrl += urlArray[j] + "|"; } string url = rootValue + arrStr[i].Replace('\\', '/'); string[] subUrl = url.Split('/'); string fileName = subUrl[subUrl.Count() - 1]; string newFileName = fileName.Substring(fileName.IndexOf("~") + 1); htmlStr += ""; htmlStr += ""; } } htmlStr += "
" + newFileName + "删除
"; } return htmlStr; } #endregion #region 附件上传 /// /// 附件上传 /// /// 上传控件 /// 上传路径 /// 定义路径 /// public static string UploadAttachment(string rootPath, FileUpload fileUpload, string fileUrl, string constUrl) { string initFullPath = rootPath + constUrl; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } string filePath = fileUpload.PostedFile.FileName; string fileName = Funs.GetNewFileName() + "~" + Path.GetFileName(filePath); int count = fileUpload.PostedFile.ContentLength; string savePath = constUrl + fileName; string fullPath = initFullPath + fileName; if (!File.Exists(fullPath)) { byte[] buffer = new byte[count]; Stream stream = fileUpload.PostedFile.InputStream; stream.Read(buffer, 0, count); MemoryStream memoryStream = new MemoryStream(buffer); FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write); memoryStream.WriteTo(fs); memoryStream.Flush(); memoryStream.Close(); fs.Flush(); fs.Close(); memoryStream = null; fs = null; if (!string.IsNullOrEmpty(fileUrl)) { fileUrl += "," + savePath; } else { fileUrl += savePath; } } else { fileUrl = string.Empty; } return fileUrl; } #endregion #region 附件删除 /// /// 附件删除 /// /// 附件路径 /// 隐藏列路径 /// public static string DeleteAttachment(string fileUrl, string hiddenUrl) { string hdAttachUrlStr = hiddenUrl; string[] urlArray = hdAttachUrlStr.Split('|'); string scanUrl = string.Empty; for (int j = 0; j < urlArray.Length; j++) { if (!string.IsNullOrEmpty(urlArray[j])) { if (j == 0) { scanUrl += urlArray[j]; } else { scanUrl += "\\" + urlArray[j]; } } } if (!String.IsNullOrEmpty(fileUrl)) { string[] arrStr = fileUrl.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries); fileUrl = null; for (int i = 0; i < arrStr.Length; i++) { if (scanUrl != arrStr[i]) { if (i != arrStr.Length - 1) { fileUrl += arrStr[i] + ","; } else { fileUrl += arrStr[i]; } } } } return fileUrl; } #endregion #region 附件打开公共方法 /// /// 显示附件文件 /// /// public static void ShowAttachmentsFile(string rootValue, string path) { if (!string.IsNullOrEmpty(path)) { string[] arrStr = path.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arrStr.Length; i++) { if (!string.IsNullOrEmpty(arrStr[i])) { string[] urlArray = arrStr[i].Split('\\'); string scanUrl = string.Empty; for (int j = 0; j < urlArray.Length; j++) { scanUrl += urlArray[j] + "|"; } string url = rootValue + arrStr[i].Replace('\\', '/'); System.Web.HttpContext.Current.Response.Write(""); } } } } #endregion #region 附件在Image中显示 /// /// 附件在Image中显示 /// /// 文件夹路径 /// 附件路径 /// 附件显示HTML public static string ShowImage(string rootValue, string path) { string htmlStr = string.Empty; if (!string.IsNullOrEmpty(path)) { htmlStr = ""; string[] arrStr = path.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arrStr.Length; i++) { if (!string.IsNullOrEmpty(arrStr[i])) { string[] urlArray = arrStr[i].Split('\\'); string scanUrl = string.Empty; for (int j = 0; j < urlArray.Length; j++) { scanUrl += urlArray[j] + "|"; } string url = rootValue + arrStr[i].Replace('\\', '/'); string[] subUrl = url.Split('/'); string fileName = subUrl[subUrl.Count() - 1]; string newFileName = fileName.Substring(fileName.IndexOf("~") + 1); htmlStr += ""; } } htmlStr += "
"; } return htmlStr; } #endregion } }