< Summary - Kestrun — Combined Coverage

Information
Class: Public.Response.Write-KrFileResponse
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrFileResponse.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 76
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 01:25:22 Line coverage: 0% (0/10) Total lines: 71 Tag: Kestrun/Kestrun@07f821172e5dc3657f1be7e6818f18d6721cf38a09/04/2025 - 22:37:32 Line coverage: 0% (0/10) Total lines: 72 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec609/12/2025 - 03:43:11 Line coverage: 0% (0/12) Total lines: 76 Tag: Kestrun/Kestrun@d160286e3020330b1eb862d66a37db2e26fc9042

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Response/Write-KrFileResponse.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Sends a file as the HTTP response.
 4
 5    .DESCRIPTION
 6        Writes a file from disk to the response body. The file path is resolved
 7        relative to the Kestrun root if required. Additional options allow
 8        specifying the download name, forcing inline display and custom content
 9        type.
 10    .PARAMETER FilePath
 11        The path to the file to send in the response. This can be an absolute path
 12        or a relative path from the Kestrun root.
 13    .PARAMETER ContentType
 14        The content type of the file being sent. If not specified, it will be determined
 15        based on the file extension.
 16    .PARAMETER StatusCode
 17        The HTTP status code to set for the response. Defaults to 200 (OK).
 18    .PARAMETER FileDownloadName
 19        The name to use for the file when downloaded. If not specified, the original
 20        file name will be used.
 21    .PARAMETER ContentDisposition
 22        Specifies how the content should be presented in the response. Options include
 23        inline and attachment.
 24    .EXAMPLE
 25        Write-KrFileResponse -FilePath "C:\path\to\file.txt" -ContentType "text/plain" -StatusCode 200 -FileDownloadName
 26        Sends the file at "C:\path\to\file.txt" as a downloadable attachment
 27        with the name "download.txt" and a content type of "text/plain". The response
 28        status code is set to 200 (OK).
 29    .NOTES
 30        This function is designed to be used in the context of a Kestrun server response.
 31#>
 32function Write-KrFileResponse {
 33    [KestrunRuntimeApi('Route')]
 34    [CmdletBinding()]
 35    param(
 36        [Parameter(Mandatory = $true)]
 37        [string]$FilePath,
 38        [Parameter()]
 39        [string]$ContentType,
 40        [Parameter()]
 41        [int]$StatusCode = 200,
 42        [Parameter()]
 43        [string]$FileDownloadName,
 44        [Parameter()]
 45        [Kestrun.Models.ContentDispositionType]$ContentDisposition = [Kestrun.Models.ContentDispositionType]::NoContentD
 46    )
 47    # Only works inside a route script block where $Context is available
 048    if ($null -ne $Context.Response) {
 49        try {
 50            # Check if the Context.Response is available
 051            if ($null -ne $Context.Response) {
 52                # Resolve the file path relative to the Kestrun root if necessary
 053                $resolvedPath = Resolve-KrPath -Path $FilePath -KestrunRoot -Test
 054                Write-KrLog -Level Verbose -Message "Resolved file path: $resolvedPath"
 55                # Set the content disposition type if specified
 056                if ($ContentDisposition -ne [Kestrun.Models.ContentDispositionType]::NoContentDisposition) {
 057                    $Context.Response.ContentDisposition.Type = $ContentDisposition.ToString()
 58                }
 59                # Set the file download name if specified
 060                if (!([string]::IsNullOrEmpty($FileDownloadName))) {
 061                    $Context.Response.ContentDisposition.FileName = $FileDownloadName
 62                }
 63
 64                # Call the C# method on the $Context.Response object
 065                $Context.Response.WriteFileResponse($resolvedPath, $ContentType, $StatusCode)
 066                Write-Information "File response written for $FilePath with download name $FileDownloadName"
 67            }
 68        } catch {
 69            # Handle any errors that occur during the file response writing
 070            Write-KrLog -Level Error -Message 'Error writing file response.' -ErrorRecord $_
 71        }
 72    } else {
 073        Write-KrOutsideRouteWarning
 74    }
 75}
 76

Methods/Properties

Write-KrFileResponse()