人民医院API

FileHelper.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. namespace RMYY_CallCenter_Api.Utility
  9. {
  10. public static class FileHelper
  11. {
  12. /// <summary>
  13. /// 上传文件
  14. /// </summary>
  15. /// <param name="hpFile">文件</param>
  16. /// <param name="path">路径</param>
  17. /// <returns></returns>
  18. public static string Upload(HttpPostedFileBase hpFile, string path)
  19. {
  20. path = HttpContext.Current.Server.MapPath("..") + path;
  21. string newName = "(" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ")" + hpFile.FileName;
  22. int iLen = hpFile.ContentLength;
  23. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  24. byte[] bData = new byte[iLen];
  25. hpFile.InputStream.Read(bData, 0, iLen);
  26. FileStream newFile = new FileStream(path + newName, FileMode.Create);
  27. newFile.Write(bData, 0, bData.Length);
  28. newFile.Flush();
  29. return newName;
  30. }
  31. /// <summary>
  32. /// 根据地址获取文件扩展名
  33. /// </summary>
  34. /// <param name="path"></param>
  35. /// <returns></returns>
  36. public static string GetExt(string path)
  37. {
  38. return Path.GetExtension(path);
  39. }
  40. /// <summary>
  41. /// 根据地址获取文件扩展名
  42. /// </summary>
  43. /// <param name="path"></param>
  44. /// <returns></returns>
  45. public static string GetPath()
  46. {
  47. return HttpContext.Current.Server.MapPath("..");
  48. }
  49. /// <summary>
  50. /// 判断附件是否存在
  51. /// </summary>
  52. /// <param name="path"></param>
  53. /// <returns></returns>
  54. public static bool Exists(string path)
  55. {
  56. return File .Exists (path);
  57. }
  58. /// <summary>
  59. /// 获取文件MD5值
  60. /// </summary>
  61. /// <param name="fileName">文件绝对路径</param>
  62. /// <returns>MD5值</returns>
  63. public static string GetFileMD5(string fileName)
  64. {
  65. try
  66. {
  67. FileStream file = new FileStream(fileName, FileMode.Open);
  68. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  69. byte[] retVal = md5.ComputeHash(file);
  70. file.Close();
  71. StringBuilder sb = new StringBuilder();
  72. for (int i = 0; i < retVal.Length; i++)
  73. {
  74. sb.Append(retVal[i].ToString("x2"));
  75. }
  76. return sb.ToString();
  77. }
  78. catch
  79. {
  80. return null;
  81. }
  82. }
  83. public static string Upload(HttpPostedFileBase hpFile, string path, string names)
  84. {
  85. path = HttpContext.Current.Server.MapPath("..") + "/" + path;
  86. string fileType = hpFile.FileName.Substring(hpFile.FileName.LastIndexOf("."));
  87. string newName = DateTime.Now.ToString("yyyyMMdd") + names + fileType;
  88. int iLen = hpFile.ContentLength;
  89. if (!Directory.Exists(path)) Directory.CreateDirectory(path);
  90. byte[] bData = new byte[iLen];
  91. hpFile.InputStream.Read(bData, 0, iLen);
  92. FileStream newFile = new FileStream(path + newName, FileMode.Create);
  93. newFile.Write(bData, 0, bData.Length);
  94. newFile.Flush();
  95. return newName;
  96. }
  97. }
  98. }