| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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
- {
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="hpFile">文件</param>
- /// <param name="path">路径</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 根据地址获取文件扩展名
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string GetExt(string path)
- {
- return Path.GetExtension(path);
- }
- /// <summary>
- /// 根据地址获取文件扩展名
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string GetPath()
- {
- return HttpContext.Current.Server.MapPath("..");
- }
- /// <summary>
- /// 判断附件是否存在
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool Exists(string path)
- {
- return File .Exists (path);
- }
- /// <summary>
- /// 获取文件MD5值
- /// </summary>
- /// <param name="fileName">文件绝对路径</param>
- /// <returns>MD5值</returns>
- 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;
- }
- }
- }
|