123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using WMS.Util;
- namespace WMS.BZServices
- {
- public class AnnexesFileService
- {
- private string cacheKey = "AnnexesFile";
- private readonly RedisService _redisService;
- public AnnexesFileService(RedisService redisService)
- {
- _redisService = redisService;
- }
- /// <summary>
- /// 保存分片附件
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="chunk">分片文件序号</param>
- /// <param name="bytes">文件流</param>
- public void SaveChunkAnnexes(string fileGuid, int chunk, byte[] bytes)
- {
- try
- {
- _redisService.Set<byte[]>(cacheKey + chunk + "_" + fileGuid, bytes,TimeSpan.FromDays(1));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 保存附件到文件中
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="filePath">文件路径</param>
- /// <param name="chunks">总共分片数</param>
- /// <param name="buffer">文件二进制流</param>
- /// <returns>-1:表示保存失败</returns>
- public DataTable SaveAnnexesToDataTable(string fileGuid, string ext, int chunks)
- {
- try
- {
- MemoryStream ms = new MemoryStream();
- for (int i = 0; i < chunks; i++)
- {
- byte[] bufferByRedis = _redisService.Get<byte[]>(cacheKey + i + "_" + fileGuid);
- if (bufferByRedis == null)
- {
- throw BZSysExCore.ThrowFailException("传输失败");
- }
- //写入二进制流
- ms.Write(bufferByRedis, 0, bufferByRedis.Length);
- _redisService.Del(cacheKey + i + "_" + fileGuid);
- }
- return ExcelHelper.ExcelImport(ms, "." + ext);
- }
- catch (Exception ex)
- {
- throw new Exception("无法识别的文件");
- }
- }
- }
- }
|