C# 下载模板文件 Excel

后端代码

cs 复制代码
[HttpGet("DownloadExcel")]
  public async Task<dynamic> DownloadExcel(string tmplName)
        {
            var _fileName = "导入表模板.xlsx";
            
            var filePath = @"Files\DownLoad\";
            var NewFile = Path.Combine(filePath, tmplName);
            var stream = new FileStream(NewFile, FileMode.Open);
            return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }

前端代码 vue 2.0

javascript 复制代码
// 下载导入表模板
    downloadExcel() {
      Axios.get('/api/Excel/DownloadExcel', {
        params: {
          tmplName: 'Excel'
        },
        responseType: 'blob',
      })
          .then(res => {
            const link = document.createElement('a')
            // const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
            const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)
            link.download = 'xxx入表模板.xlsx'
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
          })
          .catch(error => {
            console.log(error)
          })
    },

注意: 发布的WebAPI项目中有模板文件和文件夹

相关推荐
加号36 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
无风听海7 小时前
C# 隐式转换深度解析
java·开发语言·c#
LateFrames8 小时前
520 - 如何说晚安 (WPF)
c#·wpf·浪漫·ui体验
魔法阵维护师9 小时前
从零开发游戏需要学习的c#模块,第十四章(保存和加载)
学习·游戏·c#
Xin_ye1008613 小时前
C# 零基础到精通教程 - 第十一章:LINQ——语言集成查询
开发语言·c#
Xin_ye1008613 小时前
C# 零基础到精通教程 - 第十章:集合与泛型——高效管理数据
开发语言·c#
魔法阵维护师15 小时前
从零开发游戏需要学习的c#模块,第十一章(rpg小游戏入门,上篇,地图与移动)
学习·游戏·c#
雪豹阿伟15 小时前
8.C# —— 随机数、DateTime时间、字符串
c#·上位机
天下无敌笨笨熊15 小时前
C#常用三方库使用心得
开发语言·c#
魔法阵维护师16 小时前
从零开发游戏需要学习的c#模块,第十三章(rpg小游戏入门,下篇,地图敌人与战斗触发)
学习·游戏·c#