小编Van*_*nço的帖子

使用ASP.NET Core下载文件

我在创建文件后尝试下载excel文件,并且出现以下错误:

UnauthorizedAccessException:拒绝访问路径'C:\ Users\user_name\Documents\Visual Studio 2015\Projects\Project_Name\src\Project_Name\wwwroot'.

该文件已成功创建,问题出在下载方法中.

我已经尝试解决此错误,执行以下操作:

  • 打开VS作为管理员
  • 将IIS_IUSR用户添加到项目文件夹

这是代码:

    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)

c# asp.net-core

6
推荐指数
1
解决办法
6136
查看次数

标签 统计

asp.net-core ×1

c# ×1