| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Writes binary data directly to the HTTP response body. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Sends a byte array to the client. Useful for returning images or other |
| | | 6 | | binary content with a specified status code and content type. |
| | | 7 | | .PARAMETER InputObject |
| | | 8 | | The binary data to write to the response body. This should be a byte array. |
| | | 9 | | .PARAMETER StatusCode |
| | | 10 | | The HTTP status code to set for the response. Defaults to 200 (OK). |
| | | 11 | | .PARAMETER ContentType |
| | | 12 | | The content type of the response. If not specified, defaults to "application/octet-stream". |
| | | 13 | | .EXAMPLE |
| | | 14 | | Write-KrBinaryResponse -InputObject $myBinaryData -StatusCode 200 -ContentType "application/octet-stream" |
| | | 15 | | Writes the $myBinaryData to the response body with a 200 OK status code and |
| | | 16 | | content type "application/octet-stream". |
| | | 17 | | .NOTES |
| | | 18 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 19 | | #> |
| | | 20 | | function Write-KrBinaryResponse { |
| | | 21 | | [KestrunRuntimeApi('Route')] |
| | | 22 | | [CmdletBinding()] |
| | | 23 | | param( |
| | | 24 | | [Parameter(Mandatory = $true)] |
| | | 25 | | [byte[]]$InputObject, |
| | | 26 | | [Parameter()] |
| | | 27 | | [int]$StatusCode = 200, |
| | | 28 | | [Parameter()] |
| | | 29 | | [string]$ContentType |
| | | 30 | | ) |
| | | 31 | | # Only works inside a route script block where $Context is available |
| | 0 | 32 | | if ($null -ne $Context.Response) { |
| | | 33 | | # Call the C# method on the $Context.Response object |
| | 0 | 34 | | $Context.Response.WriteBinaryResponse($InputObject, $StatusCode, $ContentType) |
| | | 35 | | } else { |
| | 0 | 36 | | Write-KrOutsideRouteWarning |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |