using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace RMYY_CallCenter_Api.Utility { public static class FileHelper { /// /// 上传文件 /// /// 文件 /// 路径 /// public static string Upload(HttpPostedFileBase hpFile, string path) { path = HttpContext.Current.Server.MapPath("..") + path; string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName; int iLen = hpFile.ContentLength; if (!Directory.Exists(path)) Directory.CreateDirectory(path); byte[] bData = new byte[iLen]; hpFile.InputStream.Read(bData, 0, iLen); FileStream newFile = new FileStream(path + newName, FileMode.Create); newFile.Write(bData, 0, bData.Length); newFile.Flush(); return newName; } /// /// 根据地址获取文件扩展名 /// /// /// public static string GetExt(string path) { return Path.GetExtension(path); } /// /// 根据地址获取文件扩展名 /// /// /// public static string GetPath() { return HttpContext.Current.Server.MapPath(".."); } /// /// 判断附件是否存在 /// /// /// public static bool Exists(string path) { return File .Exists (path); } /// /// 获取文件MD5值 /// /// 文件绝对路径 /// MD5值 public static string GetFileMD5(string fileName) { try { FileStream file = new FileStream(fileName, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch { return null; } } public static string Upload(HttpPostedFileBase hpFile, string path, string names) { path = HttpContext.Current.Server.MapPath("..") + "/" + path; string fileType = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf(".")); string newName = DateTime.Now.ToString("yyyyMMdd") + names + fileType; int iLen = hpFile.ContentLength; if (!Directory.Exists(path)) Directory.CreateDirectory(path); byte[] bData = new byte[iLen]; hpFile.InputStream.Read(bData, 0, iLen); FileStream newFile = new FileStream(path + newName, FileMode.Create); newFile.Write(bData, 0, bData.Length); newFile.Flush(); return newName; } } }