如何从ASP.NET Core Web API Web应用程序返回Excel文件?

编程入门 行业动态 更新时间:2024-10-18 19:30:49
本文介绍了如何从ASP.NET Core Web API Web应用程序返回Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在类似的问题中,使用此代码可以下载PDF:

In similar questions, with this code works to download a PDF:

我正在使用Controller文件夹中的本地文件(.xlsx,.pdf,.zip)进行测试.

I'm testing with local files (.xlsx, .pdf, .zip) inside the Controller folder.

此处有类似问题

[HttpGet("downloadPDF")] public FileResult TestDownloadPCF() { HttpContext.Response.ContentType = "application/pdf"; FileContentResult result = new FileContentResult (System.IO.File.ReadAllBytes("Controllers/test.pdf"), "application/pdf") { FileDownloadName = "test.pdf" }; return result; }

但是当使用另一个文件吗?例如Excel文件(.xlsx)或ZIP文件(.zip)时,测试将无法正常进行.

But when another file?, for example an Excel File(.xlsx) or ZIP File(.zip), testing does not work properly.

代码:

[HttpGet("downloadOtherFile")] public FileResult TestDownloadOtherFile() { HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes("Controllers/test.xlsx"), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "otherfile" }; return result; }

结果:

我还对以下Content-Type进行了测试:

I also did tests with the following Content-Type:

  • 应用程序/vnd.ms-excel"
  • 应用程序/vnd.ms-excel.12"

获得相同的结果.

哪种是返回任何文件类型的正确方法?

感谢您的回答

推荐答案

我的(有效的)解决方案:

My (working) solution:

  • 我有一个使用EPPlus.Core动态创建XLSX文件的类.
    • 这将为生成的文件的路径返回FileInfo.
    • I've got a class that dynamically creates an XLSX file using EPPlus.Core.
      • This returns a FileInfo for the generated file's path.

      这就是我的控制器中的内容:

      This is what is in my Controller:

      [HttpGet("test")] public async Task<FileResult> Get() { var contentRootPath = _hostingEnvironment.ContentRootPath; // "items" is a List<T> of DataObjects var items = await _mediator.Send(new GetExcelRequest()); var fileInfo = new ExcelFileCreator(contentRootPath).Execute(items); var bytes = System.IO.File.ReadAllBytes(fileInfo.FullName); const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Response.ContentType = contentType; HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); var fileContentResult = new FileContentResult(bytes, contentType) { FileDownloadName = fileInfo.Name }; return fileContentResult; }

      这是我在Angular2中拥有的东西:

      And here is what I have in Angular2:

      downloadFile() { debugger; var headers = new Headers(); headers.append('responseType', 'arraybuffer'); let url = new URL('api/excelFile/test', environment.apiUrl); return this.http .get(url.href, { withCredentials: true, responseType: ResponseContentType.ArrayBuffer }) .subscribe((response) => { let file = new Blob([response.blob()], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); let fileName = response.headers.get('Content-Disposition').split(';')[1].trim().split('=')[1]; saveAs(file, fileName); }, err => this.errorHandler.onError(err) ); }

更多推荐

如何从ASP.NET Core Web API Web应用程序返回Excel文件?

本文发布于:2023-11-13 15:10:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   文件   Core   NET   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!