从ApplicationService基类返回文件

问题描述 投票:1回答:1

我正在尝试将名为UploadedFileEntities的SQL表中的数据导出到excel文件中,我在前端使用了angular,在后端使用了.NET Core(ASP.Net Core样板框架)。问题是由于File是Controller Base类的一个类,我无法按Application类返回文件,那么如何从ApplicationService基类返回File?

这是我的代码

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

        }


    }

如果我返回File Visual Studio,请告诉我

不可说话的成员'File'不能像方法一样使用

如果我返回新的File Visual Studio,请告诉我:

无法创建静态类'File'的实例]]

我正在尝试将名为UploadedFileEntities的SQL表中的数据导出到excel文件中,我在前端使用了angular,在后端使用了.NET Core(ASP.Net Core样板框架)。问题是我不能...

asp.net-core .net-core domain-driven-design export-to-excel aspnetboilerplate
1个回答
1
投票

文件方法是从ControllerBase.File派生而未继承的。

© www.soinside.com 2019 - 2024. All rights reserved.