在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 public class FileHelp : System.Web.UI.Page 2 { 3 4 /// <summary> 5 /// 保存文件 6 /// 返回:数组:status[0]:文件名称;status[1]:文件后缀名;status[2]:文件绝对URL;status[3]:文件相对URL; 7 /// </summary> 8 /// <param name="floder"></param> 9 /// <param name="file"></param> 10 /// <returns>数组:status[0]:文件名称;status[1]:文件后缀名;status[2]:文件URL</returns> 11 public static string[] SaveFile(FileBase.FileFloder floder, HttpPostedFile file) 12 { 13 try 14 { 15 string[] status = { "", "", "", "" }; 16 17 String Tpath = "/" + floder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/"; 18 string docName = file.FileName; 19 string dufix = Path.GetExtension(docName); 20 string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-" + System.Guid.NewGuid().ToString(); 21 status[0] = FileName + dufix; 22 status[1] = dufix; 23 string FilePath = FileBase.fileBasePath + Tpath; 24 DirectoryInfo di = new DirectoryInfo(FilePath); 25 if (!di.Exists) { di.Create(); } 26 27 file.SaveAs(FilePath + FileName + dufix); 28 29 status[2] = (FilePath + FileName + dufix).Replace("\\", "/"); 30 status[3] = (Tpath + FileName + dufix).Replace("\\", "/"); 31 32 return status; 33 } 34 catch (Exception ex) 35 { 36 throw ex; 37 } 38 } 39 40 /// <summary> 41 /// 保存文件 42 /// 返回:数组:files[0]:文件夹位置;files[1]:文件名;files[2]:文件所在位置; 43 /// </summary> 44 /// <param name="floder"></param> 45 /// <param name="tempId"></param> 46 /// <param name="file"></param> 47 /// <returns>数组:files[0]:文件夹位置;files[1]:文件名;files[2]:文件所在位置</returns> 48 public static string[] SaveFile(string filepath, HttpPostedFile file) 49 { 50 try 51 { 52 string[] files = { "", "", "" }; 53 54 String Tpath = "/" + filepath + "/"; 55 string docName = file.FileName; 56 string dufix = Path.GetExtension(docName); 57 string FileName = docName; 58 string FilePath = FileBase.fileBasePath + Tpath; 59 files[0] = FilePath; 60 DirectoryInfo di = new DirectoryInfo(FilePath); 61 if (!di.Exists) { di.Create(); } 62 63 file.SaveAs(FilePath + FileName); 64 files[1] = FileName; 65 files[2] = FilePath + FileName; 66 67 68 return files; 69 } 70 catch (Exception ex) 71 { 72 throw ex; 73 } 74 } 75 76 public static void CreateDir(string driPath) 77 { 78 DirectoryInfo di = new DirectoryInfo(FileBase.fileBasePath + "/" + driPath); 79 if (!di.Exists) { di.Create(); } 80 } 81 82 /// <summary> 83 /// 清空文件夹的文件 84 /// </summary> 85 /// <param name="floder"></param> 86 /// <param name="tempId"></param> 87 public static void ClearFolderFiles(string path) 88 { 89 try 90 { 91 string[] files = { "", "", "" }; 92 String Tpath = "/" + path + "/"; 93 string FilePath = FileBase.fileBasePath + Tpath; 94 DirectoryInfo di = new DirectoryInfo(FilePath); 95 if (di.Exists) 96 { 97 FileSystemInfo[] fileinfo = di.GetFileSystemInfos(); //返回目录中所有文件和子目录 98 foreach (FileSystemInfo i in fileinfo) 99 { 100 if (i is DirectoryInfo) //判断是否文件夹 101 { 102 DirectoryInfo subdir = new DirectoryInfo(i.FullName); 103 subdir.Delete(true); //删除子目录和文件 104 } 105 else 106 { 107 File.Delete(i.FullName); //删除指定文件 108 } 109 } 110 } 111 112 } 113 catch (Exception ex) 114 { 115 throw ex; 116 } 117 } 118 119 120 public static void RemoveFile(string dirPath,string fileName) 121 { 122 try 123 { 124 //删除文件 125 for (int i = 0; i < Directory.GetFiles(dirPath).ToList().Count; i++) 126 { 127 if (Path.GetFileName(Directory.GetFiles(dirPath)[i]) == fileName) 128 { 129 File.Delete(fileName); 130 } 131 } 132 } 133 catch (Exception ex) 134 { 135 throw ex; 136 } 137 } 138 139 140 public static void RemoveDir(string dirPath, string fileName) 141 { 142 try 143 { 144 //删除文件夹 145 for (int i = 0; i < Directory.GetDirectories(dirPath).ToList().Count; i++) 146 { 147 if (Directory.GetDirectories(dirPath)[i] == fileName) 148 { 149 Directory.Delete(fileName, true); 150 } 151 } 152 } 153 catch (Exception ex) 154 { 155 throw ex; 156 } 157 } 158 159 160 /// <summary> 161 /// 实体集合转datatable 162 /// </summary> 163 /// <typeparam name="T"></typeparam> 164 /// <param name="collection"></param> 165 /// <returns></returns> 166 public static DataTable ToDataTable<T>(IEnumerable<T> collection) 167 { 168 var props = typeof(T).GetProperties(); 169 var dt = new DataTable(); 170 dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()); 171 if (collection.Count() > 0) 172 { 173 for (int i = 0; i < collection.Count(); i++) 174 { 175 ArrayList tempList = new ArrayList(); 176 foreach (PropertyInfo pi in props) 177 { 178 object obj = pi.GetValue(collection.ElementAt(i), null); 179 tempList.Add(obj); 180 } 181 object[] array = tempList.ToArray(); 182 dt.LoadDataRow(array, true); 183 } 184 } 185 return dt; 186 } 187 188 189 190 public static FileName SaveFiles(FileBase.FileFloder floder, HttpPostedFile file) 191 { 192 try 193 { 194 FileName fileName = new SN_DEV_Help.FileName(); 195 //string status = ""; 196 String Tpath = "/" + floder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/"; 197 string docName = file.FileName; 198 string dufix = Path.GetExtension(docName); 199 string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-" + System.Guid.NewGuid().ToString(); 200 string FilePath = FileBase.fileBasePath + Tpath; 201 DirectoryInfo di = new DirectoryInfo(FilePath); 202 if (!di.Exists) { di.Create(); } 203 204 file.SaveAs(FilePath + FileName + dufix); 205 fileName.url = (FilePath + FileName + dufix).Replace("\\", "/"); 206 fileName.name = FileName; 207 return fileName; 208 } 209 catch (Exception ex) 210 { 211 throw ex; 212 } 213 } 214 215 public static FileDetails UploadFile() 216 { 217 FileDetails fileDetails = new FileDetails(); 218 HttpFileCollection filelist = HttpContext.Current.Request.Files; 219 if (filelist != null && filelist.Count > 0) 220 { 221 for (int i = 0; i < filelist.Count; i++) 222 { 223 HttpPostedFile file = filelist[i]; 224 try 225 { 226 var fileName = FileHelp.SaveFiles(FileBase.FileFloder.Institution, file); 227 fileDetails.Url = fileName.url; 228 fileDetails.name = fileName.name; 229 fileDetails.oldname = System.IO.Path.GetFileNameWithoutExtension(file.FileName); 230 fileDetails.Format = System.IO.Path.GetExtension(file.FileName); 231 fileDetails.Msg = 1; 232 } 233 catch (Exception ex) 234 { 235 fileDetails.Msg = 0; 236 } 237 } 238 } 239 else 240 { 241 fileDetails.Msg = 0; 242 fileDetails.Result = "上传的文件信息不存在!"; 243 } 244 return fileDetails; 245 } 246 247 248 public static List<FileDetails> UploadFileT()//上传多个文件 249 { 250 List<FileDetails> list = new List<SN_DEV_Help.FileDetails>(); 251 252 HttpFileCollection filelist = HttpContext.Current.Request.Files; 253 if (filelist != null && filelist.Count > 0) 254 { 255 for (int i = 0; i < filelist.Count; i++) 256 { 257 FileDetails fileDetails = new FileDetails(); 258 HttpPostedFile file = filelist[i]; 259 try 260 { 261 var fileName = FileHelp.SaveFiles(FileBase.FileFloder.Institution, file); 262 fileDetails.Url = fileName.url; 263 fileDetails.name = fileName.name; 264 fileDetails.oldname = file.FileName; 265 fileDetails.Msg = 1; 266 } 267 catch (Exception ex) 268 { 269 fileDetails.Msg = 0; 270 } 271 list.Add(fileDetails); 272 } 273 } 274 else 275 { 276 //fileDetails.Msg = 0; 277 //fileDetails.Result = "上传的文件信息不存在!"; 278 } 279 return list; 280 } 281 282 283 } 284 public class FileDetails //details 285 { 286 /// <summary> 287 /// 保存链接 288 /// </summary> 289 public string Url { get; set; } 290 291 public int Msg { get; set; } 292 293 public string Format { get; set; } 294 295 public string Result { get; set; } 296 297 public string name { get; set; } 298 public string oldname { get; set; } 299 } 300 301 public class FileName 302 { 303 public string url { get; set; } 304 public string name { get; set; } 305 }
保存文件 1 FileDetails fileDetails = FileHelp.UploadFile(); 2 if (fileDetails.Msg == 1) 3 { 4 //保存文件资源 5 documentresources file = new documentresources 6 { 7 DocName = fileDetails.oldname, 8 FileName = fileDetails.name, 9 DocSuffix = fileDetails.Format, 10 DocUrl = fileDetails.Url, 11 UpDate = DateTime.Now.ToString() 12 }; 13 dbContext.documentresources.Add(file); 14 dbContext.SaveChanges(); 15 16 } 17 else 18 { 19 dbContextTranscation.Rollback(); 20 faild.status = Result.失败; 21 faild.msg = "上传文件出错"; 22 return JsonHelper.SerializeObject(faild); 23 }
|
请发表评论