Reputation: 117
I´m trying to download a excel file after the file is created and I´m having the following error:
UnauthorizedAccessException: Access to the path 'C:\Users\user_name\Documents\Visual Studio 2015\Projects\Project_Name\src\Project_Name\wwwroot' is denied.
The file is created successfully, the problem is in the download method.
I already try to solve this error doing the following actions:
Here´s 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 didn´t make the deploy of the project in IIS.
Best Regards
Upvotes: 6
Views: 10909
Reputation: 117
I noticed two situations in my code. 1 - If I call the download method directly in my button the download happens, but the file is stored in localhost directory, something like http://localhost:3724/Area/Controller/downLoadMethod. I want the browser to open de download box and then I choose the place to store the file.
2 - If in my button I call the exportMethod and inside of the exportMethod I call the downloadMethod after the file is exported I just get a blank page, the download does't happen.
Best Regards
Upvotes: 0
Reputation: 117
I test your code and the error is gone, but the file is not being downloaded. Here is the code:
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);
}
Best Regards
Upvotes: 1
Reputation: 49769
In ASP.NET Core you should use PhysicalFileProvider
class to access the actual system's files. If you look into File providers section in the documentation:
The PhysicalFileProvider provides access to the physical file system. It wraps the System.IO.File type (for the physical provider), scoping all paths to a directory and its children. This scoping limits access to a certain directory and its children, preventing access to the file system outside of this boundary.
The following should work for you:
string wwwrootPath = _hostingEnvironment.WebRootPath;
string fileName = @"Employees.xlsx";
IFileProvider provider = new PhysicalFileProvider(wwwrootPath);
IFileInfo fileInfo = provider.GetFileInfo(fileName);
var readStream = fileInfo.CreateReadStream();
// PhysicalFileProvider.GetFileInfo
returns instance of PhysicalFileInfo
that provides implementation of IFileInfo.CreateReadStream
method.
Upvotes: 8