我在创建文件后尝试下载excel文件,并且出现以下错误:
UnauthorizedAccessException:拒绝访问路径'C:\ Users\user_name\Documents\Visual Studio 2015\Projects\Project_Name\src\Project_Name\wwwroot'.
该文件已成功创建,问题出在下载方法中.
我已经尝试解决此错误,执行以下操作:
这是代码:
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 …Run Code Online (Sandbox Code Playgroud)