onFileSelected() function handles image file selection from the user’s device, reads the file, and displays it in the browser for classification.
Function signature
Parameters
The file input change event containing the selected file in
event.target.files[0]How it works
The function performs the following steps:- Extracts the selected file from the event object
- Creates a FileReader instance to read the file
- Sets the image title to the filename
- Reads the file as a Data URL
- Sets the image source when reading is complete
- Handles any errors during the process
Code example
Implementation
Here’s the complete implementation from the source code:Error handling
The function includes a try-catch block that:- Catches any errors during file reading
- Displays an alert with “Error Reading Image” message
- Prevents the application from crashing on invalid files
Required HTML elements
The function requires the following HTML element to be present:File input configuration
For optimal results, configure your file input to accept only image files:accept="image/*" attribute ensures only image files are shown in the file picker.
Image display
The selected image is displayed with dimensions of 100x75 pixels:After an image is selected and displayed, you can call the
predict() function to classify the skin lesion.