Partial content in .NET Core MVC (for video/audio streaming)

前端 未结 2 1354
旧巷少年郎
旧巷少年郎 2020-12-31 06:09

I am trying to implement video and audio streaming on my website (to enable seeking in Chrome) and I recently found out that .NET Core 2.0 apparently provides a relatively s

相关标签:
2条回答
  • 2020-12-31 06:44

    My answer is based on Yuli Bonner, but with the adaptations so that it answers the question directly, and with Core 2.2

     public IActionResult GetFileDirect(string f)
    {
       var path = Path.Combine(Defaults.StorageLocation, f);
       var res = File(System.IO.File.OpenRead(path), "video/mp4");
       res.EnableRangeProcessing = true;
       return res;
    } 
    

    This allowed for seeking in the browser.

    0 讨论(0)
  • 2020-12-31 06:48

    There will be an enableRangeProcessing parameter added to the File method in version 2.1. For now, you need to set a switch. You can do this one of two ways:

    In runtimeconfig.json :

    {
      // Set the switch here to affect .NET Core apps
      "configProperties": {
        "Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing": "true"
      }
    }
    

    or:

     //Enable 206 Partial Content responses to enable Video Seeking from 
     //api/videos/{id}/file,
     //as per, https://github.com/aspnet/Mvc/pull/6895#issuecomment-356477675.
     //Should be able to remove this switch and use the enableRangeProcessing 
     //overload of File once 
     // ASP.NET Core 2.1 released
    
       AppContext.SetSwitch("Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing", 
       true);
    

    See ASP.NET Core GitHub Repo for details.

    0 讨论(0)
提交回复
热议问题