I have the following code (see below).
What I am trying to achieve is the following: I want to get the infos like size, name, etc, and after click the submit button...
In the complete application would be the possibility of deleting a file in the list before triggering the upload.
This works fine EXCEPT if I submit one file after another. I get the following exception:
Error: Microsoft.JSInterop.JSException: There is no file with ID 1. The file list may have changed. Error: There is no file with ID 1. The file list may have changed.
Could anyone help me?
Many thanks,
Raphaël
@page "/"
<h3>Upload PNG images</h3>
<p>
<InputFile OnChange="@OnInputFileChange" multiple />
</p>
@if (imageDataUrls.Count > 0)
{
<h4>Images</h4>
<div class="card" style="width:30rem;">
<div class="card-body">
@foreach (var imageDataUrl in imageDataUrls)
{
<img class="rounded m-1" src="@imageDataUrl" />
}
</div>
</div>
}
@if (list.Count() > 0)
{
<div class="card" style="width:30rem;">
<div class="card-body">
@foreach (var file in list)
{
<div>@file.Name - @file.ContentType - @file.Size</div>
}
</div>
</div>
}
<button @onclick="Submit">Télécharger</button>
@code {
IList<string> imageDataUrls = new List<string>();
List<IBrowserFile> list = new List<IBrowserFile>();
void OnInputFileChange(InputFileChangeEventArgs e)
{
var maxAllowedFiles = 3;
list.AddRange(e.GetMultipleFiles(maxAllowedFiles));
}
async Task Submit()
{
var format = "image/png";
foreach (var imageFile in list)
{
var resizedImageFile = await imageFile.RequestImageFileAsync(format,
100, 100);
var buffer = new byte[resizedImageFile.Size];
await resizedImageFile.OpenReadStream().ReadAsync(buffer);
var imageDataUrl =
$"data:{format};base64,{Convert.ToBase64String(buffer)}";
imageDataUrls.Add(imageDataUrl);
}
}
}