Download a file using ASP.NET Core

I am trying to load an excel file after creating the file and I have the following error:

UnauthorizedAccessException: Access to the path 'C: \ Users \ username \ Documents \ Visual Studio 2015 \ Projects \ Project_Name \ src \ Project_Name \ wwwroot' is denied.

The file was created successfully, the problem is the download method.

I am already trying to resolve this error by doing the following:

  • Open VS as administrator
  • Add user IIS_IUSR to the project folder

Here is the code:

    private readonly IHostingEnvironment _hostingEnvironment;
    public EmployeeController(ApplicationDbContext context, IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }
    public void createFile()
    {
        string wwwrootPath = _hostingEnvironment.WebRootPath;
        string fileName = @"Employees.xlsx";
        FileInfo file = new FileInfo(Path.Combine(wwwrootPath, fileName));

        if (file.Exists)
        {
            file.Delete();
            file = new FileInfo(Path.Combine(wwwrootPath, fileName));
        }
        using (ExcelPackage package = new ExcelPackage(file))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
            worksheet.Cells[1, 1].Value = "ID";
            worksheet.Cells[1, 2].Value = "Name";
            worksheet.Cells[1, 3].Value = "Gender";
            worksheet.Cells[1, 4].Value = "Salary (in $)";

            worksheet.Cells["A2"].Value = 1000;
            worksheet.Cells["B2"].Value = "Jon";
            worksheet.Cells["C2"].Value = "M";
            worksheet.Cells["D2"].Value = 5000;

            worksheet.Cells["A3"].Value = 1001;
            worksheet.Cells["B3"].Value = "Graham";
            worksheet.Cells["C3"].Value = "M";
            worksheet.Cells["D3"].Value = 10000;

            worksheet.Cells["A4"].Value = 1002;
            worksheet.Cells["B4"].Value = "Jenny";
            worksheet.Cells["C4"].Value = "F";
            worksheet.Cells["D4"].Value = 5000;

            package.Save(); 
            downloadFile(wwwrootPath);

        }  

}
    public FileResult downloadFile(string filePath)
    {
        var mimeType = "application/vnd.ms-excel";
        FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        return File(fileStream, mimeType, "Employees.xlsx");
    }

Note. I did not deploy the project to IIS.

Regards

+4
source share
3 answers

ASP.NET Core PhysicalFileProvider . :

PhysicalFileProvider . System.IO.File( ), . , .

:

string wwwrootPath = _hostingEnvironment.WebRootPath;
string fileName = @"Employees.xlsx";

IFileProvider provider = new PhysicalFileProvider(wwwrootPath);
IFileInfo fileInfo = provider.GetFileInfo(fileName);
var readStream = fileInfo.CreateReadStream();

//PhysicalFileProvider.GetFileInfo PhysicalFileInfo, IFileInfo.CreateReadStream.

+4

, , . :

public FileResult downloadFile(string filePath, string fileName)
{
        IFileProvider provider = new PhysicalFileProvider(filePath);
        IFileInfo fileInfo = provider.GetFileInfo(fileName);
        var readStream = fileInfo.CreateReadStream();
        var mimeType = "application/vnd.ms-excel";
        return File(readStream, mimeType, fileName);
}

0

. 1 - , , localhost, - http://localhost:3724/Area/Controller/downLoadMethod, , , .

2 - exportMethod exportMethod, downloadMethod . , .

0

Source: https://habr.com/ru/post/1678773/


All Articles