Skip to content

Commit 63da18d

Browse files
authored
Blazor file uploads (#19907)
1 parent 25ae02a commit 63da18d

4 files changed

Lines changed: 80 additions & 2 deletions

File tree

File renamed without changes.

aspnetcore/blazor/file-uploads.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
title: ASP.NET Core Blazor file uploads
3+
author: guardrex
4+
description: Learn how to upload files in Blazor with the InputFile component.
5+
monikerRange: '>= aspnetcore-5.0'
6+
ms.author: riande
7+
ms.custom: mvc
8+
ms.date: 09/17/2020
9+
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
10+
uid: blazor/file-uploads
11+
---
12+
# ASP.NET Core Blazor file uploads
13+
14+
By [Daniel Roth](https://github.com/danroth27)
15+
16+
Use the `InputFile` component to read browser file data into .NET code, including for file uploads. The `InputFile` component renders as an HTML input of type `file`.
17+
18+
By default, the user selects single files. Add the `multiple` attribute to permit the user to upload multiple files at once. When one or more files is selected by the user, the `InputFile` component fires an `OnChange` event and passes in an `InputFileChangeEventArgs` that provides access to the selected file list and details about each file.
19+
20+
A component that receives an image file can call the `RequestImageFileAsync` convenience method on the file to resize the image data within the browser's JavaScript runtime before the image is streamed into the app.
21+
22+
The following example demonstrates multiple image file upload in a component:
23+
24+
```razor
25+
<h3>Upload PNG images</h3>
26+
27+
<p>
28+
<InputFile OnChange="@OnInputFileChange" multiple />
29+
</p>
30+
31+
@if (imageDataUrls.Count > 0)
32+
{
33+
<h3>Images</h3>
34+
35+
<div class="card" style="width:30rem;">
36+
<div class="card-body">
37+
@foreach (var imageDataUrl in imageDataUrls)
38+
{
39+
<img class="rounded m-1" src="@imageDataUrl" />
40+
}
41+
</div>
42+
</div>
43+
}
44+
45+
@code {
46+
IList<string> imageDataUrls = new List<string>();
47+
48+
private async Task OnInputFileChange(InputFileChangeEventArgs e)
49+
{
50+
var imageFiles = e.GetMultipleFiles();
51+
var format = "image/png";
52+
53+
foreach (var imageFile in imageFiles)
54+
{
55+
var resizedImageFile = await imageFile.RequestImageFileAsync(format,
56+
100, 100);
57+
var buffer = new byte[resizedImageFile.Size];
58+
await resizedImageFile.OpenReadStream().ReadAsync(buffer);
59+
var imageDataUrl =
60+
$"data:{format};base64,{Convert.ToBase64String(buffer)}";
61+
imageDataUrls.Add(imageDataUrl);
62+
}
63+
}
64+
}
65+
```
66+
67+
To read data from a user-selected file, call `OpenReadStream` on the file and read from the returned stream. In a Blazor WebAssembly app, the data is streamed directly into the .NET code within the browser. In a Blazor Server app, the file data is streamed into .NET code on the server as the file is read from the stream.

aspnetcore/blazor/forms-validation.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ A set of built-in components are available to receive and validate user input. I
7070
| --------------- | ------------------- |
7171
| <xref:Microsoft.AspNetCore.Components.Forms.InputCheckbox> | `<input type="checkbox">` |
7272
| <xref:Microsoft.AspNetCore.Components.Forms.InputDate%601> | `<input type="date">` |
73+
| [`InputFile`](xref:blazor/file-uploads) | `<input type="file">` |
7374
| <xref:Microsoft.AspNetCore.Components.Forms.InputNumber%601> | `<input type="number">` |
7475
| [`InputRadio`](#radio-buttons) | `<input type="radio">` |
7576
| [`InputRadioGroup`](#radio-buttons) | `<input type="radio">` |
@@ -223,7 +224,7 @@ In the following example:
223224
* Additional code is executed depending on the validation result. Place business logic in the method assigned to <xref:Microsoft.AspNetCore.Components.Forms.EditForm.OnSubmit>.
224225

225226
```razor
226-
<EditForm EditContext="@editContext" OnSubmit="HandleSubmit">
227+
<EditForm EditContext="@editContext" OnSubmit="@HandleSubmit">
227228
228229
...
229230
@@ -1069,7 +1070,7 @@ public class ShipDescription
10691070
[Required]
10701071
[StringLength(40, ErrorMessage = "Description too long (40 char).")]
10711072
public string ShortDescription { get; set; }
1072-
1073+
10731074
[Required]
10741075
[StringLength(240, ErrorMessage = "Description too long (240 char).")]
10751076
public string LongDescription { get; set; }
@@ -1166,3 +1167,7 @@ When assigning a <xref:Microsoft.AspNetCore.Components.Forms.EditForm.Model> to
11661167
```csharp
11671168
private ExampleModel exampleModel = new ExampleModel();
11681169
```
1170+
1171+
## Additional resources
1172+
1173+
* <xref:blazor/file-uploads>

aspnetcore/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@
337337
href: blazor/forms-validation.md#built-in-forms-components
338338
- name: InputDate
339339
href: blazor/forms-validation.md#built-in-forms-components
340+
- name: InputFile
341+
href: blazor/file-uploads.md
340342
- name: InputNumber
341343
href: blazor/forms-validation.md#built-in-forms-components
342344
- name: InputRadio
@@ -363,6 +365,8 @@
363365
href: blazor/fundamentals/routing.md#route-templates
364366
- name: Title
365367
href: blazor/fundamentals/additional-scenarios.md#influence-html-head-tag-elements
368+
- name: Virtualize
369+
href: blazor/components/virtualization.md
366370
- name: Cascading values and parameters
367371
uid: blazor/components/cascading-values-and-parameters
368372
- name: Data binding
@@ -385,6 +389,8 @@
385389
uid: blazor/layouts
386390
- name: Forms and validation
387391
uid: blazor/forms-validation
392+
- name: File uploads
393+
uid: blazor/file-uploads
388394
- name: Call JavaScript from .NET
389395
uid: blazor/call-javascript-from-dotnet
390396
- name: Call .NET from JavaScript

0 commit comments

Comments
 (0)