Skip to content

Commit 870c9d6

Browse files
authored
[Blazor] Add cancel event support to InputFile component (#64772)
1 parent 0e0b241 commit 870c9d6

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Components/Web.JS/src/InputFile.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ function init(callbackWrapper: any, elem: InputElement): void {
5252

5353
callbackWrapper.invokeMethodAsync('NotifyChange', fileList);
5454
});
55+
56+
// The 'cancel' event is fired when the user cancels the file picker dialog.
57+
// This event is part of the HTML5 standard and is supported in modern browsers.
58+
// For browsers that don't support this event, it will be silently ignored.
59+
elem.addEventListener('cancel', function(): void {
60+
// Notify with an empty list when the file dialog is cancelled.
61+
elem._blazorFilesById = {};
62+
callbackWrapper.invokeMethodAsync('NotifyChange', []);
63+
});
5564
}
5665

5766
async function toImageFile(elem: InputElement, fileId: number, format: string, maxWidth: number, maxHeight: number): Promise<BrowserFile> {

src/Components/test/E2ETest/Tests/InputFileTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ public void ThrowsWhenOversizedFileIsSelected()
196196
Browser.Equal("Supplied file with size 32 bytes exceeds the maximum of 10 bytes.", () => exceptionMessage.Text);
197197
}
198198

199+
[Fact]
200+
public void CanClearFilesByInvokingCancelEvent()
201+
{
202+
// Upload a file first
203+
var file = TempFile.Create(_tempDirectory, "txt", "This is a test file.");
204+
var inputFile = Browser.Exists(By.Id("input-file"));
205+
inputFile.SendKeys(file.Path);
206+
207+
// Verify the file was uploaded
208+
var fileContainer = Browser.Exists(By.Id($"file-{file.Name}"));
209+
210+
// Get the file count element
211+
var fileCount = Browser.Exists(By.Id("file-count"));
212+
Browser.Equal("1", () => fileCount.Text);
213+
214+
// Trigger the cancel event via JavaScript to simulate canceling the file dialog
215+
Browser.ExecuteJavaScript(@"
216+
const inputElement = document.getElementById('input-file');
217+
inputElement.dispatchEvent(new Event('cancel'));
218+
");
219+
220+
// Wait a moment for the event to be processed and verify the file list was cleared
221+
Browser.Equal("0", () => Browser.Exists(By.Id("file-count")).Text);
222+
}
223+
199224
public void Dispose()
200225
{
201226
Directory.Delete(_tempDirectory, recursive: true);

src/Components/test/testassets/BasicTestApp/FormsTest/InputFileComponent.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Max allowed files:
1616
<InputFile OnChange="LoadFiles" id="input-file" multiple />
1717
<br />
1818

19+
<span id="file-count">@loadedFiles.Count</span>
1920
<span id="exception-message">@exceptionMessage</span>
2021

2122
@if (isLoading)

0 commit comments

Comments
 (0)