11@using System .IO ;
2- @using Microsoft .AspNetCore .Components .Forms
2+ @using Microsoft .AspNetCore .Components .Forms
33
44<h1 >File preview</h1 >
55
6- Max file size:<br />
7- <input type =" number" id =" max-file-size" @bind-value =" @maxFileSize" /><br />
6+ Max file size:
7+ <br />
8+ <input type =" number" id =" max-file-size" @bind-value =" @maxFileSize" />
9+ <br />
810
9- Max allowed files:<br />
10- <input type =" number" id =" max-allowed-files" @bind-value =" @maxAllowedFiles" /><br />
11+ Max allowed files:
12+ <br />
13+ <input type =" number" id =" max-allowed-files" @bind-value =" @maxAllowedFiles" />
14+ <br />
1115
12- <InputFile OnChange =" LoadFiles" MaxFileSize =" maxFileSize" MaxAllowedFiles =" maxAllowedFiles" id =" input-file" multiple /><br />
16+ <InputFile OnChange =" LoadFiles" id =" input-file" multiple />
17+ <br />
1318
1419<span id =" exception-message" >@exceptionMessage </span >
1520
1621@if (isLoading )
1722{
18- <p >Loading .. .</p ><br />
23+ <p >Loading .. .</p >
24+ <br />
1925}
2026
2127@foreach ( var (file , content ) in loadedFiles )
@@ -29,7 +35,8 @@ Max allowed files:<br />
2935
3036<h1 >Image upload</h1 >
3137
32- <InputFile OnChange =" LoadImage" id =" input-image" /><br />
38+ <InputFile OnChange =" LoadImage" id =" input-image" />
39+ <br />
3340
3441@if (imageDataUri != null )
3542{
@@ -64,16 +71,16 @@ Max allowed files:<br />
6471
6572 try
6673 {
67- foreach (var file in e .Files )
74+ foreach (var file in e .GetMultipleFiles ( maxAllowedFiles ) )
6875 {
6976 StateHasChanged ();
7077
71- using var reader = new StreamReader (file .OpenReadStream ());
78+ using var reader = new StreamReader (file .OpenReadStream (maxFileSize ));
7279
7380 loadedFiles .Add (file , await reader .ReadToEndAsync ());
7481 }
7582 }
76- catch (InvalidOperationException ex )
83+ catch (Exception ex )
7784 {
7885 exceptionMessage = ex .Message ;
7986 }
@@ -83,19 +90,13 @@ Max allowed files:<br />
8390
8491 async Task LoadImage (InputFileChangeEventArgs e )
8592 {
86- var file = e .Files .SingleOrDefault ();
93+ var format = " image/jpeg" ;
94+ var imageFile = await e .File .RequestImageFileAsync (format , 640 , 480 );
8795
88- if (file != null )
89- {
90- var format = " image/jpeg" ;
91- var imageFile = await file .RequestImageFileAsync (format , 640 , 480 );
92-
93- using var fileStream = imageFile .OpenReadStream ();
94- using var memoryStream = new MemoryStream ();
95- await fileStream .CopyToAsync (memoryStream );
96+ using var fileStream = imageFile .OpenReadStream (maxFileSize );
97+ using var memoryStream = new MemoryStream ();
98+ await fileStream .CopyToAsync (memoryStream );
9699
97- imageDataUri = $" data:{format };base64,{Convert .ToBase64String (memoryStream .ToArray ())}" ;
98- StateHasChanged ();
99- }
100+ imageDataUri = $" data:{format };base64,{Convert .ToBase64String (memoryStream .ToArray ())}" ;
100101 }
101102}
0 commit comments