Skip to content

Commit 217c723

Browse files
committed
Updates
1 parent 6142ef1 commit 217c723

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

aspnetcore/blazor/file-uploads.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ Rendered HTML:
4545
> [!NOTE]
4646
> In the preceding example, the `<input>` element's `_bl_2` attribute is used for Blazor's internal processing.
4747
48-
To read data from a user-selected file from a <xref:System.IO.Stream> that represents the file's bytes, call <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile.OpenReadStream%2A?displayProperty=nameWithType> on the file and read from the returned stream. For more information, see the [File streams](#file-streams) section.
48+
To read data from a user-selected file with a <xref:System.IO.Stream> that represents the file's bytes, call <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile.OpenReadStream%2A?displayProperty=nameWithType> on the file and read from the returned stream. For more information, see the [File streams](#file-streams) section.
4949

5050
<xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile.OpenReadStream%2A> enforces a maximum size in bytes of its <xref:System.IO.Stream>. Reading one file or multiple files larger than 500 KB results in an exception. This limit prevents developers from accidentally reading large files into memory. The `maxAllowedSize` parameter of <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile.OpenReadStream%2A> can be used to specify a larger size if required.
5151

52-
For scenarios other than processing a small file, such as saving a thumbnail or avatar to a database, avoid reading the incoming file stream directly into memory all at once. For example, don't copy all of the file's bytes into a <xref:System.IO.MemoryStream> or read the entire stream into a byte array all at once. These approaches can result in degraded app performance and potential [Denial of Service (DoS)](xref:blazor/security/interactive-server-side-rendering#denial-of-service-dos-attacks) risk, especially for server-side components. Instead, consider adopting either of the following approaches:
52+
For scenarios other than processing a small file, such as saving a small image to a database, avoid reading the incoming file stream directly into memory all at once. For example, don't copy all of the file's bytes into a <xref:System.IO.MemoryStream> or read the entire stream into a byte array all at once. These approaches can result in degraded app performance and potential [Denial of Service (DoS)](xref:blazor/security/interactive-server-side-rendering#denial-of-service-dos-attacks) risk, especially for server-side components. Instead, consider adopting either of the following approaches:
5353

5454
* Copy the stream directly to a file on disk without reading it into memory. Note that Blazor apps executing code on the server aren't able to access the client's file system directly.
5555
* Upload files from the client directly to an external service. For more information, see the [Upload files to an external service](#upload-files-to-an-external-service) section.
5656

5757
In the following examples, `browserFile` implements <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile> to represent an uploaded file. Working implementations for <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile> are shown in the file upload components later in this article.
5858

59-
When calling <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile.OpenReadStream%2A>, we recommend passing a maximum allowed file size in `maxAllowedSize` at the limit of the file sizes that you expect, which is the maximum number of bytes that can be supplied by the <xref:System.IO.MemoryStream> with a default value of 500 KB. The examples in this article use a local variable or constant assignment (usually not shown) named `maxFileSize`.
59+
When calling <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile.OpenReadStream%2A>, we recommend passing a maximum allowed file size in the `maxAllowedSize` parameter at the limit of the file sizes that you expect to receive. The default value is 500 KB. This article's examples use a maximum allowed file size variable or constant named `maxFileSize` but usually don't show setting a specific value.
6060

6161
<span aria-hidden="true">✔️</span><span class="visually-hidden">Supported:</span> The following approach is **recommended** because the file's <xref:System.IO.Stream> is provided directly to the consumer, a <xref:System.IO.FileStream> that creates the file at the provided path:
6262

@@ -892,15 +892,15 @@ The following pattern:
892892
* Is based on the [Blazor movie database tutorial app](xref:blazor/tutorials/movie-database-app/index).
893893
* Can be enhanced with additional code for file size and content type [validation feedback](xref:blazor/forms/validation).
894894

895-
For the following example to work in a Blazor Web App (ASP.NET Core 8.0 or later), the component must adopt an [interactive render mode](xref:blazor/fundamentals/index#static-and-interactive-rendering-concepts) (for example, `@rendermode InteractiveServer`) to call `HandleSelectedThumbnail` on an `InputFile` file change. Blazor Server app components are always interactive and don't require a render mode.
895+
For the following example to work in a Blazor Web App (ASP.NET Core 8.0 or later), the component must adopt an [interactive render mode](xref:blazor/fundamentals/index#static-and-interactive-rendering-concepts) (for example, `@rendermode InteractiveServer`) to call `HandleSelectedThumbnail` on an `InputFile` component file change (`OnChange` parameter/event). Blazor Server app components are always interactive and don't require a render mode.
896896

897897
In the following example, a small thumbnail (<= 100 KB) in an <xref:Microsoft.AspNetCore.Components.Forms.IBrowserFile> is saved to a database with EF Core. If a file isn't selected by the user for the `InputFile` component, a default thumbnail is saved to the database.
898898

899899
The default thumbnail (`default-thumbnail.jpg`) is at the project root with a **Copy to Output Directory** setting of **Copy if newer**:
900900

901901
![Default generic thumbnail image](~/blazor/file-uploads/_static/default-thumbnail.jpg)
902902

903-
The `Movie` model (`Movie.cs`) has a byte array property, named `Thumbnail`, to hold the thumbnail image's bytes:
903+
The `Movie` model (`Movie.cs`) has a byte array property (`Thumbnail`) to hold the thumbnail image's bytes:
904904

905905
```csharp
906906
public byte[]? Thumbnail { get; set; }
@@ -929,15 +929,17 @@ In the following `Create` component, an image upload is processed. You can enhan
929929
930930
<div class="row">
931931
<div class="col-md-4">
932-
<EditForm method="post" Model="Movie" OnValidSubmit="AddMovie" FormName="create" Enhance>
932+
<EditForm method="post" Model="Movie" OnValidSubmit="AddMovie"
933+
FormName="create" Enhance>
933934
<DataAnnotationsValidator />
934935
<ValidationSummary class="text-danger" role="alert"/>
935936
936937
...
937938
938939
<div class="mb-3">
939940
<label for="thumbnail" class="form-label">Thumbnail:</label>
940-
<InputFile id="thumbnail" OnChange="HandleSelectedThumbnail" class="form-control" />
941+
<InputFile id="thumbnail" OnChange="HandleSelectedThumbnail"
942+
class="form-control" />
941943
</div>
942944
<button type="submit" class="btn btn-primary">Create</button>
943945
</EditForm>

0 commit comments

Comments
 (0)