AnnexesFileService.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using WMS.Util;
  8. namespace WMS.BZServices
  9. {
  10. public class AnnexesFileService
  11. {
  12. private string cacheKey = "AnnexesFile";
  13. private readonly RedisService _redisService;
  14. public AnnexesFileService(RedisService redisService)
  15. {
  16. _redisService = redisService;
  17. }
  18. /// <summary>
  19. /// 保存分片附件
  20. /// </summary>
  21. /// <param name="fileGuid">文件主键</param>
  22. /// <param name="chunk">分片文件序号</param>
  23. /// <param name="bytes">文件流</param>
  24. public void SaveChunkAnnexes(string fileGuid, int chunk, byte[] bytes)
  25. {
  26. try
  27. {
  28. _redisService.Set<byte[]>(cacheKey + chunk + "_" + fileGuid, bytes,TimeSpan.FromDays(1));
  29. }
  30. catch (Exception ex)
  31. {
  32. throw ex;
  33. }
  34. }
  35. /// <summary>
  36. /// 保存附件到文件中
  37. /// </summary>
  38. /// <param name="fileGuid">文件主键</param>
  39. /// <param name="filePath">文件路径</param>
  40. /// <param name="chunks">总共分片数</param>
  41. /// <param name="buffer">文件二进制流</param>
  42. /// <returns>-1:表示保存失败</returns>
  43. public DataTable SaveAnnexesToDataTable(string fileGuid, string ext, int chunks)
  44. {
  45. try
  46. {
  47. MemoryStream ms = new MemoryStream();
  48. for (int i = 0; i < chunks; i++)
  49. {
  50. byte[] bufferByRedis = _redisService.Get<byte[]>(cacheKey + i + "_" + fileGuid);
  51. if (bufferByRedis == null)
  52. {
  53. throw BZSysExCore.ThrowFailException("传输失败");
  54. }
  55. //写入二进制流
  56. ms.Write(bufferByRedis, 0, bufferByRedis.Length);
  57. _redisService.Del(cacheKey + i + "_" + fileGuid);
  58. }
  59. return ExcelHelper.ExcelImport(ms, "." + ext);
  60. }
  61. catch (Exception ex)
  62. {
  63. throw new Exception("无法识别的文件");
  64. }
  65. }
  66. }
  67. }