分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

.net 打包下载

发布时间:2023-09-06 02:06责任编辑:林大明关键词:暂无标签
 

ZipArchive 打包下载

private IActionResult DownloadZipFromUrl(string[] guids,string zipFullName) ???????{ ???????????using (MemoryStream zipStream = new MemoryStream()) ???????????{ ???????????????using (System.Net.WebClient webClient = new System.Net.WebClient()) ???????????????{ ???????????????????using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, leaveOpen: true)) ???????????????????{ ???????????????????????foreach (var m in guids) ???????????????????????{ ???????????????????????????if (string.IsNullOrWhiteSpace(m)) continue; ???????????????????????????if (m == Guid.Empty.ToString()) continue; ???????????????????????????#region build url : https://****/upload/image?g=353e7e1b-69ae-4a60-8cfc-3737c2a64eaa&j=false ???????????????????????????var builder = new UriBuilder() ???????????????????????????{ ???????????????????????????????Scheme = Request.Scheme, ???????????????????????????????Host = Request.Host.Host, ???????????????????????????????Path = "upload/image", ???????????????????????????????Query = "j=false&g=" + m, ???????????????????????????}; ???????????????????????????if (Request.Host.Port != null) ???????????????????????????{ ???????????????????????????????builder.Port = Request.Host.Port.Value; ???????????????????????????} ???????????????????????????#endregion ???????????????????????????webClient.DownloadDataCompleted += wc_DownloadDataCompleted; ???????????????????????????var attachmentData = webClient.DownloadData(builder.Uri); ???????????????????????????ZipArchiveEntry entry = archive.CreateEntry(string.IsNullOrWhiteSpace(_DownloadAttachmentFileName) ? "File1.pdf" : _DownloadAttachmentFileName, System.IO.Compression.CompressionLevel.Fastest); ???????????????????????????using (var entryStream = entry.Open()) ???????????????????????????{ ???????????????????????????????entryStream.Write(attachmentData); ???????????????????????????} ???????????????????????} ???????????????????} ???????????????}// disposal of archive will force data to be written to memory stream. ???????????????zipStream.Position = 0; //reset memory stream position. ???????????????return File(zipStream.ToArray(), "application/vnd.ms-excel", zipFullName); ???????????} ???????}

  

获取文件名

private string _DownloadAttachmentFileName = string.Empty; ???????private void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) ???????{ ???????????WebClient wc = sender as WebClient; ???????????// Try to extract the filename from the Content-Disposition header ???????????if (!String.IsNullOrEmpty(wc.ResponseHeaders["Content-Disposition"])) ???????????{ ???????????????_DownloadAttachmentFileName = wc.ResponseHeaders["Content-Disposition"].Substring(wc.ResponseHeaders["Content-Disposition"].IndexOf("filename=") + 10).Replace("\"", ""); //FileName ok ??????????????????????????} ???????????var data = e.Result; //File OK ???????}

  

.net 打包下载

原文地址:https://www.cnblogs.com/panpanwelcome/p/9373662.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved