File Reader
Use cases:
- Reading and processing files client-side without uploading to server
- Parsing JSON files for client-side data processing
- File validation and content extraction
The file-reader component can be used to select and process files from the local file system. It primarily supports JSON files and provides automatic parsing capabilities. The component processes files client-side without uploading them to a server, making it ideal for client-side data processing scenarios.
Example
- Genesis
- React
- Angular
Declaration
<rapid-file-reader></rapid-file-reader>
Usage
// Define the event detail types
interface FileReadEventDetail {
fileName: string;
content: any;
file: File;
}
interface ErrorEventDetail {
message: string;
error?: any;
}
@customElement({
name: 'my-element',
template: html`
<rapid-file-reader
label="Select JSON File"
accept=".json,application/json"
file-size-limit-bytes="10485760"
auto-process="false"
processing-delay-ms="1000"
@file-read=${(x, ctx) => x.handleFileRead(ctx.event)}
@error=${(x, ctx) => x.handleError(ctx.event)}
@clear=${(x) => x.handleClear()}
>
</rapid-file-reader>
`,
})
export class MyElement extends GenesisElement {
handleFileRead(event: CustomEvent<FileReadEventDetail>) {
const { fileName, content, file } = event.detail;
console.log('File processed:', fileName, content);
}
handleError(event: CustomEvent<ErrorEventDetail>) {
console.error('File processing error:', event.detail);
}
handleClear() {
console.log('File cleared');
}
}
Declaration
<rapid-file-reader></rapid-file-reader>
Usage
// Define the event detail types
interface FileReadEventDetail {
fileName: string;
content: any;
file: File;
}
interface ErrorEventDetail {
message: string;
error?: any;
}
export function MyComponent() {
const handleFileRead = (event: CustomEvent<FileReadEventDetail>) => {
const { fileName, content, file } = event.detail;
console.log('File processed:', fileName, content);
};
const handleError = (event: CustomEvent<ErrorEventDetail>) => {
console.error('File processing error:', event.detail);
};
const handleClear = () => {
console.log('File cleared');
};
return (
<rapid-file-reader
label="Select JSON File"
accept=".json,application/json"
file-size-limit-bytes="10485760"
auto-process="false"
processing-delay-ms="1000"
onFileRead={handleFileRead}
onError={handleError}
onClear={handleClear}
>
</rapid-file-reader>
);
}
Declaration
<rapid-file-reader></rapid-file-reader>
Usage
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
// Define the event detail types
interface FileReadEventDetail {
fileName: string;
content: any;
file: File;
}
interface ErrorEventDetail {
message: string;
error?: any;
}
@Component({
selector: 'my-root',
template: `
<rapid-file-reader
label="Select JSON File"
accept=".json,application/json"
file-size-limit-bytes="10485760"
auto-process="false"
processing-delay-ms="1000"
(fileRead)="handleFileRead($event)"
(error)="handleError($event)"
(clear)="handleClear()"
>
</rapid-file-reader>
`,
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [FormsModule],
})
export class AppComponent {
handleFileRead(event: CustomEvent<FileReadEventDetail>) {
const { fileName, content, file } = event.detail;
console.log('File processed:', fileName, content);
}
handleError(event: CustomEvent<ErrorEventDetail>) {
console.error('File processing error:', event.detail);
}
handleClear() {
console.log('File cleared');
}
}
The file-reader component provides a user-friendly interface for file selection and processing. It includes:
- File Selection: Browse button to select files from the local file system
- File Validation: Automatic validation of file type and size
- Processing State: Visual feedback during file processing
- Content Parsing: Automatic JSON parsing for supported file types
- Error Handling: Comprehensive error handling with user notifications
API
Property and attribute binding examples for Genesis Component syntax.
Attributes
| Name | Type | Description | Example |
|---|---|---|---|
| label | string | This attribute specifies the caption for the file input, similar to HTML Label element. There is no default value specified for label. | |
| accept | string | This attribute determines the file formats that the file input should be able to select. Multiple file formats can be specified by separating them with a comma. By default, only JSON files are accepted: .json,application/json. | |
| fileSizeLimitBytes | string | This attribute specifies the maximum file size in bytes that is supported by the file input. The default value is 10_485_760 (10MB). | |
| autoProcess | boolean | This attribute determines whether the file should be automatically processed when selected. When true, the file is processed immediately after selection. When false, a "Process file" button is shown. The default value is false. | |
| processingDelayMs | string | This attribute specifies the delay in milliseconds before hiding the processing state after file processing is complete. The default value is 1000 (1 second). | |
Properties
| Name | Type | Description | Example |
|---|---|---|---|
| fileContent | any | This property contains the parsed content of the processed file. For JSON files, this will be the parsed JavaScript object. It is null when no file is processed. | |
| onFileRead | (data: any) => void | This property is a callback function that is called when a file is successfully processed. The function receives the parsed file content as a parameter. | |
Slots
This component doesn't have any slots.
Parts
| Part | Use |
|---|---|
| file-name-input | The text field showing the selected file name. |
| select-file-button | The browse button for file selection. |
| process-file-button | The process file button (shown when auto-process is false). |
| processing-overlay | The overlay shown during file processing. |
| clear-icon | The clear icon for removing file selection. |
Events fired
| Event | Type | Description | Example |
|---|---|---|---|
| fileRead | Event | Emits when a file is successfully processed. The event detail contains fileName, content, and file properties. | |
| error | Event | Emits when there is an error during file processing, validation, or parsing. | |
| clear | Event | Emits when the file selection is cleared. | |
Events listened to
This component doesn't listen to any events.
Methods
| Name | Parameters | Return Type | Description | Example |
|---|---|---|---|---|
| handleFileProcessing | None | Promise<void> | Manually triggers file processing. Useful when auto-process is disabled. | |
| getFileContent | None | any | Returns the parsed content of the processed file. | |
| getSelectedFile | None | File | null | Returns the currently selected File object. | |
| clearSelection | None | void | Clears the current file selection and resets the component state. | |
Limitations
note
- Currently supports single file selection only
- Primarily designed for JSON file processing
- File processing is done client-side only
- Future versions will support multiple files and additional file types
Best Practices
- File Type Validation: Always specify appropriate file types in the
acceptattribute - Size Limits: Set reasonable file size limits to prevent performance issues
- Error Handling: Implement proper error handling for file processing failures
- User Feedback: Use the processing delay to provide visual feedback during file operations
- Auto-processing: Use auto-process for immediate feedback, manual processing for user control