多文件下载

1.下载文件  这行注释的   不知道msSystem对象是什么   不开这行注释也可以下载

        [AllowAnonymousAttribute]
        [Route("DownloadResourceFiles")]
        [HttpGet]
        public async Task<IActionResult> DownloadResourceFiles(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return NotFound();
            }
            string controller = RouteData.Values["controller"].ToString();
            var path = @"C:JCB";
            var memoryStream = new MemoryStream();
            using (var stream = new FileStream(path + fileName + ".xml", FileMode.Open))
            {
                await stream.CopyToAsync(memoryStream);
            }
            memoryStream.Seek(0, SeekOrigin.Begin);
            //文件名必须编码,否则会有特殊字符(如中文)无法在此下载。
            //string encodeFilename = msSystem.Web.HttpUtility.UrlEncode(fileName, msSystem.Text.Encoding.GetEncoding("UTF-8"));
            Response.Headers.Add("Content-Disposition", "attachment; filename=" + fileName + ".xml");
            return new FileStreamResult(memoryStream, "application/octet-stream");//文件流方式,指定文件流对应的ContenType。
        }

2.如果是单一的文件  前端可以直接

window.open(url);
3.如果前端需要循环下载多个文件   直接用window.open 下载的时候 后面的文件会被前面的冲突  导致只有打开一个下载链接
通过下面这种方式就可以一次性打开多个下载链接  
      let url = 'http://localhost:60275/api/Download/DownloadResourceFiles?fileName=' + item.code;
      let iframe = document.createElement('iframe') as any ;
      iframe.style.display = 'none'; // 防止影响页面
      iframe.style.height = 0; // 防止影响页面
      iframe.src = url;
      document.body.appendChild(iframe);//这一行必须  ifroma挂载到dom树上才会发请求
    //1分钟之后删除元素 setTimeout(()
=> { iframe.remove(); }, 60 * 1000);

https://segmentfault.com/a/1190000016771027

原文地址:https://www.cnblogs.com/jiangchengbiao/p/11134510.html