StreamFlex.AspNetCore 1.0.5

dotnet add package StreamFlex.AspNetCore --version 1.0.5
                    
NuGet\Install-Package StreamFlex.AspNetCore -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StreamFlex.AspNetCore" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StreamFlex.AspNetCore" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="StreamFlex.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StreamFlex.AspNetCore --version 1.0.5
                    
#r "nuget: StreamFlex.AspNetCore, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StreamFlex.AspNetCore@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StreamFlex.AspNetCore&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=StreamFlex.AspNetCore&version=1.0.5
                    
Install as a Cake Tool

StreamFlex.AspNetCore – ASP.NET Core Streaming Utility

Overview

StreamFlex.AspNetCore is a lightweight, production-ready utility for efficiently streaming large files or data streams (e.g., videos, documents) over HTTP in ASP.NET Core Web APIs.

It provides support for:

  • Range requests for seeking through large files (e.g., videos)
  • Automatic MIME type detection based on file extensions
  • Streaming directly from streams (not just file paths)
  • Customizable response headers, including optional Content-Disposition for downloads

Installation

You can install the package from NuGet:

dotnet add package StreamFlex.AspNetCore

Usage

Controller Example – Stream Video

[HttpGet("video/{fileName}")]
public async Task<IActionResult> StreamVideo(string fileName, CancellationToken ct)
{
    try
    {
        var filePath = Path.Combine("Media", "Videos", fileName);

        await FileStreamingHelper.StreamFileAsync(
            HttpContext,
            filePath,
            downloadFileName: fileName,
            enableRangeProcessing: true,
            cancellationToken: ct
        );

        return new EmptyResult();
    }
    catch (OperationCanceledException) when (ct.IsCancellationRequested)
    {
        _logger.LogInformation("Video stream was canceled by the client: {FileName}", fileName);
        return new EmptyResult();
    }
}

Controller Example – Download Document

[HttpGet("document/{fileName}")]
public async Task<IActionResult> DownloadFile(string fileName, CancellationToken ct)
{
    try
    {
        var filePath = Path.Combine("Media", "Documents", fileName);

        await FileStreamingHelper.StreamFileAsync(
            HttpContext,
            filePath,
            downloadFileName: fileName,
            enableRangeProcessing: false,
            cancellationToken: ct
        );

        return new EmptyResult();
    }
    catch (OperationCanceledException) when (ct.IsCancellationRequested)
    {
        _logger.LogInformation("Document download was canceled by the client: {FileName}", fileName);
        return new EmptyResult();
    }
}

API Reference

StreamFileAsync(...)

Task StreamFileAsync(
    HttpContext context,
    string filePath,
    string? contentType = null,
    string? downloadFileName = null,
    bool enableRangeProcessing = true,
    Action<HttpResponse>? configureResponse = null,
    CancellationToken cancellationToken = default
)

Streams a file from the given file path to the HTTP response with optional range support.

StreamAsync(...)

Task StreamAsync(
    HttpContext context,
    Stream sourceStream,
    long totalLength,
    string contentType = "application/octet-stream",
    string? downloadFileName = null,
    bool enableRangeProcessing = true,
    Action<HttpResponse>? configureResponse = null,
    CancellationToken cancellationToken = default
)

Streams from any provided Stream, useful for blob storage or in-memory data.

HTML5 Video Player Example

You can use the API to stream video directly in HTML5 players:

<video width="720" controls>
  <source src="https://yourdomain.com/api/media/video/sample.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

This enables seeking through the video stream.

Security Considerations

  • Validate input to ensure file paths are safe and do not allow directory traversal.
  • Limit access to certain folders (e.g., private content) via authorization checks.
  • Rate-limit or throttle requests for large files to avoid excessive load.

License

StreamingFileHandler is licensed under the MIT License. See the LICENSE file for more details.

🛠️ Contributing

Feel free to fork the repository, file issues, or submit pull requests for improvements.

📄 Changelog

v1.0.0 - Initial release with support for file streaming and HTTP range requests.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 154 5/18/2025
1.0.4 146 5/18/2025
1.0.3 232 5/13/2025
1.0.2 139 5/11/2025
1.0.1 137 5/11/2025
1.0.0 139 5/11/2025