Skip to content

MudFileUpload: fix AppendMultipleFiles keeping stale file references (#9586)#9600

Merged
henon merged 9 commits intoMudBlazor:devfrom
igotinfected:fix/file-upload-append-multiple-files
Oct 15, 2024
Merged

MudFileUpload: fix AppendMultipleFiles keeping stale file references (#9586)#9600
henon merged 9 commits intoMudBlazor:devfrom
igotinfected:fix/file-upload-append-multiple-files

Conversation

@igotinfected
Copy link
Copy Markdown
Member

@igotinfected igotinfected commented Aug 9, 2024

Description

Resolves #9586
Resolves #9795

Fixed an issue where uploading files multiple times with AppendMultipleFiles and attempting to read the file contents would throw an exception indicating that those files no longer exist.

Problem explained in #9586 (comment).

Whats it boils down to is that when using AppendMultipleFiles in MudFileUpload we append "new" files to our list of "old" files. The references to old files however no longer exist in the HTML input, causing an error when trying to read those files.

Currently users have to disable AppendMultipleFiles and make use of the file references with every upload.

The fix proposed here consists creating new InputFile instances with every upload, so that the previous file references are retained. This implementation implies the following:

  • there are multiple file input controls in HTML, each with their unique id
  • only one input is "active" at a time
  • all but the newest inputs are destroyed after ClearAsync is called
  • users can no longer provide their own input ids since we now have multiple inputs at once (⚠️ could be considered a breaking change)

This also fixes an issue where the FilesChanged callback would not be triggered when uploading the same file twice (despite the file contents having changed) (#9795).

How Has This Been Tested?

Visually with test components and with some basic unit tests.

Type of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (fix or improvement to the website or code docs)

Checklist

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

@github-actions github-actions bot added the bug Unexpected behavior or functionality not working as intended label Aug 9, 2024
@igotinfected
Copy link
Copy Markdown
Member Author

It could be possible to test against this by playing around with IBrowserFiles, will try to look into that soon.

Need to do a bunch of regression testing for this since it messes with multiple HTML inputs.

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.80%. Comparing base (28bc599) to head (5d607ce).
Report is 582 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #9600      +/-   ##
==========================================
+ Coverage   89.82%   90.80%   +0.97%     
==========================================
  Files         412      406       -6     
  Lines       11878    12830     +952     
  Branches     2364     2485     +121     
==========================================
+ Hits        10670    11650     +980     
+ Misses        681      625      -56     
- Partials      527      555      +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@igotinfected
Copy link
Copy Markdown
Member Author

Can't test against the actual bug with bUnit since it's a browser error (in JS). Will do more manual regression testing before publishing the PR.

@igotinfected
Copy link
Copy Markdown
Member Author

Ready for review. I will do some more some manual testing on all browsers today to make sure I didn't miss any edge cases.

@igotinfected igotinfected marked this pull request as ready for review September 22, 2024 10:13
Copy link
Copy Markdown
Member

@Mr-Technician Mr-Technician left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending the one change

Comment thread src/MudBlazor.UnitTests.Viewer/Properties/launchSettings.json
@igotinfected igotinfected added the breaking change This change will require consumer code updates (ex: removes/changes a public API) label Oct 4, 2024
@ScarletKuro ScarletKuro requested a review from henon October 4, 2024 08:26
@ScarletKuro
Copy link
Copy Markdown
Member

@henon should be this moved to v8 or this breaking change is fine?

@henon
Copy link
Copy Markdown
Contributor

henon commented Oct 4, 2024

Let's play this safe and release it with v8. This is not urgent is it? And we have decided to start the v8 process now anyway, so it won't be long.

@henon henon added the v8 label Oct 4, 2024
@igotinfected
Copy link
Copy Markdown
Member Author

Nope not urgent, I prefer playing it safe too, thanks!

@ScarletKuro
Copy link
Copy Markdown
Member

Do we merge this in v8? @henon

@henon henon merged commit eca6997 into MudBlazor:dev Oct 15, 2024
@henon
Copy link
Copy Markdown
Contributor

henon commented Oct 15, 2024

Yes! Thanks @igotinfected

@henon
Copy link
Copy Markdown
Contributor

henon commented Oct 15, 2024

@igotinfected What would you say we should write in the migration guide about this?

@igotinfected
Copy link
Copy Markdown
Member Author

@igotinfected What would you say we should write in the migration guide about this?

(The fix description)
Fixed an issue where uploading files multiple times with AppendMultipleFiles and attempting to read the file contents would throw an exception indicating that those files no longer exist.

(The breaking change description)
The component now generates a new input with every file change to be able to retain references to previous files. As such, it is no longer possible to supply your own id to the underlying input.

... or something similar!

@igotinfected igotinfected deleted the fix/file-upload-append-multiple-files branch October 15, 2024 08:39
@henon
Copy link
Copy Markdown
Contributor

henon commented Oct 15, 2024

So what should people do who rely on that id? What workaround can we propose to them?

@igotinfected
Copy link
Copy Markdown
Member Author

Hmm I can't think of a scenario where you would absolutely need the id of the underlying input other than for automated UI testing purposes. You could use a different selector instead, for example by adding your own custom attribute like data-test-id that would be applied to all inputs though with the current implementation, so you'd have to do something like:

@* razor code *@
<MudFileUpload data-test-id="my-test-id" ...
// UI test code
var inputs = document.querySelectorAll('[data-test-id="my-test-id"]');
var activeInput = inputs[inputs.length - 1];
...

We can probably mess around with the @attributes if needed to only apply it to the active input if it causes issues for people down the line but I don't think it will be necessary.

@henon
Copy link
Copy Markdown
Contributor

henon commented Oct 15, 2024

Thanks, I think this explanation is enough. People can find it when they follow the link to this issue.

@henon henon mentioned this pull request Oct 15, 2024
@henon
Copy link
Copy Markdown
Contributor

henon commented Oct 15, 2024

Added to the v8 migration guide at #9953

LLauter pushed a commit to cannellamedia/MudBlazor that referenced this pull request Dec 16, 2024
@alfred0dev
Copy link
Copy Markdown

alfred0dev commented Jan 2, 2025

I did update to version MudBlazor 7.15.0 through nuget package but error still persist, anything i should do?

@igotinfected
Copy link
Copy Markdown
Member Author

I did update to version 7.15.0 from nuget package but error still persist, anything i should do?

This will only be available with v8, you could try it out with one of the released v8 previews in the meantime!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change This change will require consumer code updates (ex: removes/changes a public API) bug Unexpected behavior or functionality not working as intended

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

MudFileUpload does not fire FilesChanged if one tries to upload the same file again. Issue with Multiple File Upload in MudFileUpload Component

5 participants