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项目中有模板文件和文件夹

相关推荐
上官浩仁41 分钟前
springboot excel 表格入门与实战
java·spring boot·excel
Hello.Reader1 小时前
从零到一上手 Protocol Buffers用 C# 打造可演进的通讯录
java·linux·c#
浪扼飞舟2 小时前
c#基础(一)
开发语言·c#
一个帅气昵称啊4 小时前
C# .NET EFCore 性能优化
性能优化·c#·.net
ArabySide4 小时前
【C#】理解.NET内存机制:堆、栈与装箱拆箱的底层逻辑及优化技巧
c#
sali-tec6 小时前
C# 基于halcon的视觉工作流-章32-线线测量
开发语言·人工智能·算法·计算机视觉·c#
我好喜欢你~7 小时前
C#---Expression(表达式)
开发语言·c#
白菜上路7 小时前
C# Web API Mapster基本使用
前端·c#
LostXerxes7 小时前
封装与属性
c#
LostXerxes7 小时前
C#类与对象
c#