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

相关推荐
逝水无殇3 小时前
C# 字符串(String)详解
开发语言·后端·c#
小巧的砖头4 小时前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#
医疗信息化王工8 小时前
从零到一:基于 GGUF 格式部署 Unlimited-OCR 搭建企业级证件识别服务
图像处理·c#·ocr
zyh______9 小时前
C#语法糖(按照实用性排序)
unity·c#
z落落10 小时前
C#五大加密算法(AES、DES、MD5、RSA、SHA)
开发语言·c#
wtsolutions10 小时前
Excel-to-JSON本地化Excel插件发布 - 在Excel中安全离线转换数据
安全·json·excel
孝顺的书包10 小时前
将《C# 调用非托管程序》一文中最后一种方法修改如下(篇幅原因简化了注释):
开发语言·c#
辰三11 小时前
统信 UOS + GBase 8s 国产化环境部署实战:从虚拟机到数据库连接完整指南
linux·c#
标致的钢铁侠11 小时前
c# 温故而知新: 线程篇(一)
java·jvm·c#
用户2986985301411 小时前
Python 实现 Excel 与 TXT 文本的高效转换与导出
后端·python·excel