成功asp.net core下载文件

后台:
 [HttpGet("DownLoadRequest")]
        public FileResult downloadRequest()
        {
            string webRootPath = Directory.GetCurrentDirectory();//根目录
            var filePath = $"\\RegFile\\Client\\";//文件保存目录
            #region 获取客户端ip地址
            HttpContextAccessor context = new HttpContextAccessor();
            string ClientIp = context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            #endregion
            string FileName = webRootPath + filePath + ClientIp + ".reg";
            if(!System.IO.File.Exists(FileName))
            {
                throw new Exception("要下载文件不存在!");
            }
            var stream = System.IO.File.OpenRead(FileName);  //创建文件流
            return File(stream, "application/x-sh", "fsd" + ".reg");
        }
前台:
 DownLoadFile() {
      window.open("http://xxx/api/xxx/downloadRequest");
    },

(0)

相关推荐