/// <summary> /// 文件下载(返回Base64) /// </summary> /// <returns>文件Base64</returns> public DownloadFileRes DownloadFileBase64(OpenFileModel model) { DownloadFileRes downloadFile = new DownloadFileRes(); try { var downloadRes = GetDownloadFileStream(model); if (downloadRes.ErrorCode > 0) { downloadFile.ErrorCode = downloadRes.ErrorCode; downloadFile.ErrorDetail = downloadRes.ErrorDetail; return(downloadFile); } using (MemoryStream memstream = new MemoryStream()) { const int bufferLen = 10 * 1024 * 1024; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = downloadRes.Stream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, count); } downloadFile.FileValue = Convert.ToBase64String(memstream.ToArray()); } downloadFile.ErrorCode = 0; return(downloadFile); } catch (Exception ex) { _log.Debug($"DownloadFileBase64 Exception: {ex.Message}"); throw ex; } }
/// <summary> /// 文件下载(返回路径) /// </summary> /// <returns>文件路径</returns> public DownloadFileRes FileDownload(OpenFileModel model, string savePath) { DownloadFileRes downloadFile = new DownloadFileRes(); try { var downloadRes = GetDownloadFileStream(model); if (downloadRes.ErrorCode > 0) { downloadFile.ErrorCode = downloadRes.ErrorCode; downloadFile.ErrorDetail = downloadRes.ErrorDetail; return(downloadFile); } if (!string.IsNullOrEmpty(model.Rev)) { savePath = $"{savePath}\\{model.Rev}"; } //如果不存在,创建它 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } // 文件全路径 string filePath = $"{savePath}\\{downloadRes.FileName}"; //写入数据 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) { const int bufferLen = 10 * 1024 * 1024; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = downloadRes.Stream.Read(buffer, 0, bufferLen)) > 0) { fs.Write(buffer, 0, count); } fs.Dispose(); } downloadFile.FileValue = filePath; downloadFile.ErrorCode = 0; return(downloadFile); } catch (Exception ex) { _log.Debug($"FileDownload Exception: {ex.Message}"); throw ex; } }