C#批量打包下载文件
2022/1/7 17:08:34
本文主要是介绍C#批量打包下载文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、获取要下载文件的ID
function btn_downfile() { var rows = $("#gridTable").datagrid('getChecked'); if (rows.length == 0) { dialogMsg("至少勾选一条数据", 1); return false; } var fileIds = ""; for (var i = 0; i < rows.length; i++) { fileIds += rows[i]["AttachmentID"] + ","; } fileIds = fileIds.substr(0, fileIds.length - 1); $("#formexcel").remove(); var $form = $("<form id='formexcel' method='post' action='/HR/Employee/DownDocumentFile' style='display:none;'>"); var $input = $("<input type='hidden' name='FileIds' value='" + fileIds + "'>"); $("body").append($form); $form.append($input); $form.submit(); }
2、根据文件ID获取文件的请求地址
/// <summary> /// 文件打包下载 /// </summary> /// <param name="EmpNo">工号</param> /// <param name="FileIds">文件ID</param> /// <returns></returns> [HttpPost] public ActionResult DownDocumentFile(string EmpNo, string FileIds) { if (string.IsNullOrEmpty(FileIds)) { return Error("请勾选数据!"); } if (string.IsNullOrEmpty(EmpNo)) { EmpNo = "file"; } AttachmentBiz attBiz = new AttachmentBiz(); var fileList = attBiz.GetListEntity(FileIds);//根据文件ID获取文件地址数据 if (fileList.Count > 0) { var streamList= GetFileStreamList(fileList); //zip 文件名称 var zipFileName = EmpNo + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip"; MemoryStream ms = new MemoryStream(); byte[] buffer; using (ZipFile file = ZipFile.Create(ms)) { file.BeginUpdate(); foreach (var item in streamList) { StreamDataSource sd = new StreamDataSource(item.FileStream); file.Add(sd, item.Name); } file.CommitUpdate(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); } return File(ms.ToArray(), "application/zip", zipFileName); } else { throw new Exception("文件不能为空!"); } }
3、通过请求文件地址获取文件流
/// <summary> /// 获取文件流 /// </summary> /// <param name="list"></param> private List<DownloadModel> GetFileStreamList(List<AttachmentEntity> list) { try { EmployeeBiz employeeBiz = new EmployeeBiz(); var empList = employeeBiz.GetAllEmployeeList();//获取用户数据 List<DownloadModel> downloadList = new List<DownloadModel>(); foreach (var item in list) { var empentity = empList.Where(x => x.UserID == item.DocumentID).First(); string filename = item.AttachmentType + "_" + empentity.EmployeeNo + "_" + empentity.FullNameCN + "_" + item.Title; WebRequest req = WebRequest.Create(item.Url); req.Method = "GET"; //读取到内存 MemoryStream stmMemory = new MemoryStream(); using (WebResponse webRes = req.GetResponse()) { int length = (int)webRes.ContentLength; HttpWebResponse response = webRes as HttpWebResponse; Stream stream = response.GetResponseStream(); byte[] buffer = new byte[length]; int i; //将字节逐个放入到Byte中 while ((i = stream.Read(buffer, 0, buffer.Length)) > 0) { stmMemory.Write(buffer, 0, i); } } DownloadModel downloadModel = new DownloadModel(); downloadModel.ID = Guid.NewGuid().ToString(); downloadModel.Name = filename; downloadModel.FileStream = stmMemory; downloadList.Add(downloadModel); } return downloadList; } catch (System.Exception ex) { throw ex; } }
这篇关于C#批量打包下载文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#