using System.IO; namespace BLL { /// /// 上传附件相关 /// public class UploadFileService { #region 附件上传 /// /// 附件上传 /// /// 上传控件 /// 上传路径 /// 定义路径 /// public static string UploadAttachment(string rootPath, FineUIPro.FileUpload fileUpload, string fileUrl, string constUrl) { if (!string.IsNullOrEmpty(fileUrl)) ////是否存在附件 存在则删除 { string urlFullPath = rootPath + fileUrl; if (File.Exists(urlFullPath)) { File.Delete(urlFullPath); } } 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; //} fileUrl = savePath; } else { fileUrl = string.Empty; } return fileUrl; } #endregion #region 附件资源删除 /// /// 附件资源删除 /// /// /// public static void DeleteFile(string rootPath, string fileUrl) { if (!string.IsNullOrEmpty(fileUrl)) { string[] strs = fileUrl.Trim().Split(','); foreach (var item in strs) { string urlFullPath = rootPath + item; if (File.Exists(urlFullPath)) { File.Delete(urlFullPath); } } } } #endregion #region 上传文件路径 /// /// 版本附件路径 /// public const string SysVersionFilePath = "FileUpload\\SysVersion\\"; /// /// 焊工二维码上传路径 /// public const string QRCodeImageFilePath = "FileUpload\\Technical\\Welder\\"; #endregion } }