6

When I try to upload files from a list I get this error

"Error: There is no file with ID 1. The file list may have changed"

Its working when I attach one file but, when the list has more than one file, I get the error

The phone Im using to send is

calling function

            foreach (var item in fileList)
            {
                var Enow = item.GetMultipleFiles();
                foreach (var _item in Enow)
                {
                    output = await _IfileUpload.Upload(_item, NewGuid.ToString());
                }
            }

called function

    public async Task<string> Upload(IBrowserFile entry, string UploadGuid)
    {
        try
        {
            var path = Path.Combine(Directory.GetCurrentDirectory(), "Uploads/" + UploadGuid, entry.Name);
            var _path = Path.Combine(Directory.GetCurrentDirectory(), "Uploads/" + UploadGuid);
            if (!Directory.Exists(_path))
            {
                System.IO.Directory.CreateDirectory(_path);
            }

            Stream stream = entry.OpenReadStream();
            FileStream fs = File.Create(path);
            await stream.CopyToAsync(fs);
            stream.Close();
            fs.Close();

            return path;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
hawlk5
  • 61
  • 3

3 Answers3

4

BlazorInputFile results in error: "There is no file with ID 1". On re-add new files all previously created objects belonging to previous files aren't saved. Meantime the index starts with unsaved objects that no longer exist. Saving files at each selection step gives an correct index. The Chrome inside debugger indicates a problem in inputfile.js

sharpist
  • 41
  • 2
1

I ran into similar problem and the solution was to create image data list that is populated each time new file is added from browser. In this case you can have proper grasp on data

   private List<byte[]> _imageData = new List<byte[]>();

   private void UploadFiles(InputFileChangeEventArgs e)
        {
            foreach (var file in e.GetMultipleFiles())
            {
                _files.Add(file);
                _imageData.Add(GetImageBytes(file).Result);
        }
    }    


    private async Task<byte[]> GetImageBytes(IBrowserFile file)
    {
        var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
        await using var fileStream = new FileStream(path, FileMode.Create);
        await file.OpenReadStream(file.Size).CopyToAsync(fileStream);
        var bytes = new byte[file.Size];
        fileStream.Position = 0;
        await fileStream.ReadAsync(bytes);
        fileStream.Close();
        File.Delete(path);
        return bytes;
    }

And finally

        if (_imageData != null && _imageData.Count() > 0)
        {
            foreach (var photo in _imageData)
            {
                var result = await _uploadService.UploadImage(photo);
            }                 
        }
Adlorem
  • 1,457
  • 1
  • 11
  • 10
0

I would try this sample from the [docs][1]

<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>
}

@code {
    IList<string> imageDataUrls = new List<string>();

    private async Task OnInputFileChange(InputFileChangeEventArgs e)
    {
        var maxAllowedFiles = 3;
        var format = "image/png";

        foreach (var imageFile in e.GetMultipleFiles(maxAllowedFiles))
        {
            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);
        }
    }
}
Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • 1
    the assumption from what i have deduced is that when you upload multiple files at the same time, its actually working . But when you add one file at a time to a list thats when its bringing the error . Its like the Lists are different some house. – hawlk5 Nov 13 '20 at 12:26
  • did you try setting a breakpoint in the server code and stepping through it? – Greg Gum Nov 13 '20 at 13:49