本文介绍了如何从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文件?
发布评论