using System.IO; namespace BLL { /// <summary> /// 上传附件相关 /// </summary> public class UploadFileService { #region 附件上传 /// <summary> /// 附件上传 /// </summary> /// <param name="fileUpload">上传控件</param> /// <param name="fileUrl">上传路径</param> /// <param name="constUrl">定义路径</param> /// <returns></returns> 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 附件资源删除 /// <summary> /// 附件资源删除 /// </summary> /// <param name="rootPath"></param> /// <param name="fileUrl"></param> 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 上传文件路径 /// <summary> /// 版本附件路径 /// </summary> public const string SysVersionFilePath = "FileUpload\\SysVersion\\"; /// <summary> /// 焊工二维码上传路径 /// </summary> public const string QRCodeImageFilePath = "FileUpload\\Technical\\Welder\\"; #endregion } }