Return 来自 ApplicationService Base 的文件 Class

Return a File From ApplicationService Base Class

我正在尝试将 SQL table 中名为 UploadedFileEntities 的数据导出到 excel 文件中 我在前端使用 angular,在后端使用 .NET 核心( ASP.Net 核心样板框架)。 问题是我不能 return 应用程序 class 的文件,因为文件是控制器基础 class 的 class, 那么我如何 return ApplicationService Base class 中的文件?

这是我的代码

using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OfficeOpenXml;
using PHC.Entities;
using PHC.EntityFrameworkCore;
using PHC.MySystemServices.MyApplicationServices.DTO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using IdentityModel.Client;
using Ninject.Activation;
using DocumentFormat.OpenXml.ExtendedProperties;

namespace PHC.MySystemServices.MyApplicationServices
{
    public class MyApplicationAppService : AsyncCrudAppService<MyApplication, MyApplicationDto, int, PagedAndSortedResultRequestDto, MyApplicationDto>
    {
        private readonly IDbContextProvider<PHCDbContext> _dbContextProvider;
        private PHCDbContext db => _dbContextProvider.GetDbContext();

        private readonly IRepository<MyApplication,int> _repository;

        private IHostingEnvironment _env;
        public MyApplicationAppService(IDbContextProvider<PHCDbContext> dbContextProvider, IRepository<MyApplication, int> repository, IHostingEnvironment hostingEnvironment) :base(repository)
        {
            _repository = repository;
            _dbContextProvider = dbContextProvider;
            this._env = hostingEnvironment;
        }

   public async Task<IActionResult> ExportV2(CancellationToken cancellationToken)
        {
            // query data from database  
            await Task.Yield();
            var list = db.UploadedFileEntities.ToList();
            var stream = new MemoryStream();
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // this is important
            using (var package = new ExcelPackage(stream))
            {
                var workSheet = package.Workbook.Worksheets.Add("Sheet1");
                workSheet.Cells.LoadFromCollection(list, true);
                package.Save();
            }
            stream.Position = 0;
            string excelName = $"UserList-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx";

            //return File(stream, "application/octet-stream", excelName);  
            return new File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelName); //it doesn't work

        }


    }

如果我 return 文件 Visual studio 给我看

不可调用成员'File'不能像方法一样使用

如果我 return 新文件 Visual studio 告诉我:

无法创建静态实例 class 'File'

文件方法派生自ControllerBase.File,但您不继承它。

你可以试试FileStreamResult喜欢

return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 
{ 
    FileDownloadName = excelName
};