From 2787d1dc52066b8bff9de7bc0b58108db8224205 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Tue, 12 Jul 2022 06:14:22 -0500 Subject: [PATCH] Improve download from stream example (#26384) --- aspnetcore/blazor/file-downloads.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/aspnetcore/blazor/file-downloads.md b/aspnetcore/blazor/file-downloads.md index c3d9379f0f..2ff8f39d7a 100644 --- a/aspnetcore/blazor/file-downloads.md +++ b/aspnetcore/blazor/file-downloads.md @@ -76,6 +76,20 @@ The following example component: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/file-downloads/FileDownload1.razor"::: +For a component in a Blazor Server app that must return a for a physical file, the component can call , as the following example demonstrates: + +```csharp +private Stream GetFileStream() +{ + return File.OpenRead(@"{PATH}"); +} +``` + +In the preceding example, the `{PATH}` placeholder is the path to the file. The `@` prefix indicates that the string is a [*verbatim string literal*](/dotnet/csharp/programming-guide/strings/#verbatim-string-literals), which permits the use of backslashes (`\`) in a Windows OS path and embedded double-quotes (`""`) for a single quote in the path. Alternatively, avoid the string literal (`@`) and use either of the following approaches: + +* Use escaped backslashes (`\\`) and quotes (`\"`). +* Use forward slashes (`/`) in the path, which are supported across platforms in ASP.NET Core apps, and escaped quotes (`\"`). + ## Download from a URL *This section applies to files that are relatively large, typically 250 MB or larger.*