3

I'm trying to export data in SQL table called UploadedFileEntities into excel file I use angular in front-end and .NET core in Backend (ASP.Net Core boilerplate framework). the problem is I can't return a file by Application class because of File is a class for Controller Base class, So how could I return File from ApplicationService Base class?

Here is my code

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

        }


    }

if I return File Visual studio show me

non-invocable member 'File' cannot be used like a method

if I return new File Visual studio show me:

Cannot create an instance of the static class 'File'

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Abdulaziz
  • 654
  • 3
  • 12
  • 37
  • 1
    The `file` method is derived from [ControllerBase.File](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.file?view=aspnetcore-2.2) while you do not inherit it.Maybe you could try `FileStreamResult`:`return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = excelName };` – Ryan Mar 19 '20 at 05:40
  • @Xing Zou, thank you, Actually I found another solution I'll share it as an answer, but your suggestion works properly, So if you put your comment as an answer I'll give you the points – Abdulaziz Mar 19 '20 at 09:24
  • I am glad that you have resolved it :).Done – Ryan Mar 19 '20 at 09:31

1 Answers1

3

The file method is derived from ControllerBase.File while you do not inherit it.

You could try FileStreamResult like

return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 
{ 
    FileDownloadName = excelName
};
Ryan
  • 19,118
  • 10
  • 37
  • 53