使用ASP.NET Core下载文件

时间:2017-06-08 09:47:59

标签: c# 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 $)";

            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");
    }

注意:我没有在IIS中部署项目。

最好的问候

3 个答案:

答案 0 :(得分:4)

在ASP.NET Core中,您应该使用PhysicalFileProvider类来访问实际系统的文件。如果您查看文档中的File providers部分:

  

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实例。

答案 1 :(得分:0)

我测试了你的代码并且错误消失了,但是文件没有被下载。这是代码:

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);
}

最好的问候

答案 2 :(得分:0)

我在代码中发现了两种情况。 1 - 如果我直接在我的按钮中调用下载方法,则会发生下载,但该文件存储在localhost目录中,类似于http://localhost:3724/Area/Controller/downLoadMethod。我希望浏览器打开de download box然后选择存储文件的位置。

2 - 如果在我的按钮中我调用exportMethod并在exportMethod内部我在导出文件后调用downloadMethod我只是得到一个空白页面,下载确实没有发生。

最好的问候