Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

How to upload multiple files on form submit? #36

Open
bdfin opened this issue May 15, 2020 · 4 comments
Open

How to upload multiple files on form submit? #36

bdfin opened this issue May 15, 2020 · 4 comments

Comments

@bdfin
Copy link

bdfin commented May 15, 2020

First of all, thanks for your effort on this component.

I'm trying to upload multiple files on a form submit to Azure blob storage. Everything works perfectly if only one file is submitted but more than one causes this 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 at getFileById (https://localhost:5010/_content/BlazorInputFile/inputfile.js:116:19)

However I can see for sure that all files exist as they should in the list with the correct ids.

Here is my handle change and onsubmit:

`private void HandleChangeSelected(IFileListEntry[] files)
{
FileValidationMessage = string.Empty;

        IFileListEntry file = files.FirstOrDefault();

        if (UploadIsValid(file))
        {
            Files.Add(file);
        }
    }

    private async Task HandleValidSubmitAsync()
    {
        await AdvertService.CreateAdvertAsync(Advert);

        foreach (IFileListEntry file in Files)
        {
            await FileService.UploadImageAsync(file, Advert.Id, Vendor.Id);
        }
    }`

The error is thrown reading on the ReadAllAsync() method. Any ideas?

@vantascloud
Copy link

vantascloud commented May 19, 2020

ok, for me, this turned out to be something silly, I was getting this error because InputFile tag was inside an if statement when a property of IFileListEntry[] had a zero count, and when it was greater than zero it was effectively destroying the InputFile tag and thus the file input elements. I moved the InputFile tag outside the if statement and it worked.

@Jarrich
Copy link

Jarrich commented Feb 14, 2021

Did you ever manage to find the root cause, @bdfin ?

@bdfin
Copy link
Author

bdfin commented Feb 14, 2021

Did you ever manage to find the root cause, @bdfin ?

No I ended up using a different soloution. This has actually been brought into the standard Blazor edit form components now so I'm wondering if it was fixed before release? Not sure though as I haven't tested.

@valentasm1
Copy link

I have this very bad solution where i load all file to byte[] and then upload on submit.
How achieve scenario where use select couple files one by one and the upload them un submit?

private List<byte[]> _addedFiles = new List<byte[]>();
private async Task SingleUpload(InputFileChangeEventArgs arg)
{
	long maxFileSize = 1024 * 1024 * 15;//15MB
	var data = arg.File.OpenReadStream(maxFileSize);
	using var memoryString = new MemoryStream();
	await data.CopyToAsync(memoryString);

	_addedFiles.Add(memoryString.GetBuffer());	
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants