【问题标题】:InvalidOperationException on File return文件返回时出现 InvalidOperationException
【发布时间】:2019-05-06 22:56:56
【问题描述】:

当我尝试返回要下载的文件时遇到了一些奇怪的问题, 所以这是我的代码

string filePath = Path.Combine(Path1, Path2, filename);
return File(filePath, "audio/mp3", "myfile.mp3");

但它返回此错误的问题

InvalidOperationException:没有配置文件提供程序来处理提供的文件。

我不确定我错过了什么,有什么帮助吗?

【问题讨论】:

  • 这是你下载方法的完整代码吗?

标签: c# asp.net asp.net-mvc asp.net-core .net-core


【解决方案1】:

您可以简单地从以下位置更改您的代码:

string filePath = Path.Combine(Path1, Path2, filename);
return File(filePath, "audio/mp3", "myfile.mp3");

到这里:

string filePath = Path.Combine(Path1, Path2, filename);
return PhysicalFile(filePath, "audio/mp3", "myfile.mp3");

那么问题就解决了!

您也可以将文件放在wwwroot 文件夹(您的web 根目录 文件夹)下。然后,您可以使用 web root 文件夹的相对路径,并将filePath 放入File 方法的第一个参数中。然后您可以毫无问题地访问该文件。这将比使用PhysicalFile 方法安全得多。

【讨论】:

    【解决方案2】:

    所以返回 File 方法的方法,正如@SeM 建议的那样,但通过从文件路径中删除文件名。

    string filePath = Path.Combine(Path1, Path2);
    
    IFileProvider provider = new PhysicalFileProvider(filePath);
    IFileInfo fileInfo = provider.GetFileInfo(filename);
    var readStream = fileInfo.CreateReadStream();
    
    return File(readStream, "audio/mpeg", fileName);
    

    【讨论】:

      【解决方案3】:

      您首先需要创建文件提供程序注册。

      services.AddSingleton<IFileProvider>(  
             new PhysicalFileProvider(Directory.GetCurrentDirectory()));  
      

      那么你可以这样使用它

      public class IndexModel : PageModel
      {
          private readonly IFileProvider _fileProvider;
      
          public IndexModel(IFileProvider fileProvider)
          {
              _fileProvider = fileProvider;
          }
      
          public IFileInfo FileInfo { get; private set; }
      
          public void OnGet()
          {
              IFileInfo = _fileProvider.GetFileInfo("filename.ext");
          }
      }   
      

      在你的情况下,你的函数体会像

      string filePath = Path.Combine(Path1, Path2, filename);
      IFileInfo = _fileProvider.GetFileInfo(filepath);
      var fs = fileInfo.CreateReadStream();
      return File(fs, "audio/mp3", "myfile.mp3");
      

      【讨论】:

      • 感谢您的回复,但是有点困惑,,,“返回文件()”在哪里以及 DirectoryContents 与返回文件方法有什么关系?
      【解决方案4】:

      在asp.net core中你需要PhysicalFileProvider来访问物理文件系统:

      string filePath = Path.Combine(Path1, Path2, filename);
      
      IFileProvider provider = new PhysicalFileProvider(filePath);
      IFileInfo fileInfo = provider.GetFileInfo(filename);
      var readStream = fileInfo.CreateReadStream();
      
      return File(readStream, "audio/mpeg", fileName);
      

      据我所知,.mp3 文件的 MIME 类型是 audio/mpeg

      【讨论】:

      • 不知道为什么,但它返回给我一个错误,处理请求时发生未处理的异常。
      • 在:IFileProvider provider = new PhysicalFileProvider(filePath);
      • 处理请求时发生未处理的异常。
      • ArgumentException: 目录名 'C:\folder\file.mp3\' 不存在。参数名称:path System.IO.FileSystemWatcher..ctor(string path, string filter)
      • 您的filename 值是多少? "文件.mp3\" ?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多