gpt4 book ai didi

asp.net-core - 从 ApplicationService 基类返回一个文件

转载 作者:行者123 更新时间:2023-12-04 14:15:22 24 4
gpt4 key购买 nike

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

这是我的代码

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”不能像方法一样使用

如果我返回新文件,Visual Studio 会告诉我:

无法创建静态类"file"的实例

最佳答案

文件方法是从 ControllerBase.File 派生的,但您没有继承它。

你可以像这样尝试 FileStreamResult

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

关于asp.net-core - 从 ApplicationService 基类返回一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60738275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com