.net 打包下载

 

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
        }

  

 

转载于:https://www.cnblogs.com/panpanwelcome/p/9373662.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值