首页 > 编程语言 >asp.net core 动态生成文件下载

asp.net core 动态生成文件下载

时间:2022-08-19 06:22:06浏览次数:61  
标签:core asp stream hostingEnvironment System var path net public

一、创建action

 public class importController : BaseController
    {

        private IWebHostEnvironment _hostingEnvironment = null;
        public importController(IWebHostEnvironment hostingEnvironment )
        {
            _hostingEnvironment = hostingEnvironment;
          
        }


        public IActionResult downLoadMuban()
        {

            string path = _hostingEnvironment.ContentRootPath + "/Up/muban.xls";

                var stream = System.IO.File.OpenRead(path);

          
                string fileExt = System.IO.Path.GetExtension(path);
                //获取文件的ContentType
                var provider = new FileExtensionContentTypeProvider();
                var memi = provider.Mappings[fileExt];
                return File(stream, memi, Path.GetFileName(path));
            

        }
}

二、创建超链接

<a href="/import/downLoadMuban">模板</a>

完成!!

标签:core,asp,stream,hostingEnvironment,System,var,path,net,public
From: https://www.cnblogs.com/sunzzhankui/p/16600720.html

相关文章