feat: add file upload component (#655)
The component allow upload the same file multiple times
This commit is contained in:
committed by
László Monda
parent
65ea786358
commit
8e20c85e07
@@ -14,11 +14,9 @@
|
|||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<label class="btn btn-default btn-file">
|
<file-upload (fileChanged)="changeFile($event)"
|
||||||
Import device configuration
|
label="Import device configuration">
|
||||||
<input type="file"
|
</file-upload>
|
||||||
(change)="changeFile($event)">
|
|
||||||
</label>
|
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<button class="btn btn-danger"
|
<button class="btn btn-danger"
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
SaveUserConfigInBinaryFileAction,
|
SaveUserConfigInBinaryFileAction,
|
||||||
SaveUserConfigInJsonFileAction
|
SaveUserConfigInJsonFileAction
|
||||||
} from '../../../store/actions/user-config';
|
} from '../../../store/actions/user-config';
|
||||||
|
import { UploadFileData } from '../../../models/upload-file-data';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'device-settings',
|
selector: 'device-settings',
|
||||||
@@ -42,16 +43,7 @@ export class DeviceConfigurationComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
changeFile(event): void {
|
changeFile(data: UploadFileData): void {
|
||||||
const files = event.srcElement.files;
|
this.store.dispatch(new LoadUserConfigurationFromFileAction(data));
|
||||||
const fileReader = new FileReader();
|
|
||||||
fileReader.onloadend = function () {
|
|
||||||
const arrayBuffer = new Uint8Array(fileReader.result);
|
|
||||||
this.store.dispatch(new LoadUserConfigurationFromFileAction({
|
|
||||||
filename: event.srcElement.value,
|
|
||||||
data: Array.from(arrayBuffer)
|
|
||||||
}));
|
|
||||||
}.bind(this);
|
|
||||||
fileReader.readAsArrayBuffer(files[0]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,15 +30,10 @@
|
|||||||
(click)="onUpdateFirmware()">
|
(click)="onUpdateFirmware()">
|
||||||
Flash firmware {{ (getAgentVersionInfo$ | async).firmwareVersion }} (bundled with Agent)
|
Flash firmware {{ (getAgentVersionInfo$ | async).firmwareVersion }} (bundled with Agent)
|
||||||
</button>
|
</button>
|
||||||
<label class="btn btn-primary btn-file"
|
<file-upload [disabled]="flashFirmwareButtonDisbabled$ | async"
|
||||||
[class.disabled]="flashFirmwareButtonDisbabled$ | async">
|
(fileChanged)="changeFile($event)"
|
||||||
Choose firmware file and flash it
|
accept=".tar.bz2"
|
||||||
<input id="firmware-file-select"
|
label="Choose firmware file and flash it"></file-upload>
|
||||||
type="file"
|
|
||||||
accept=".tar.bz2"
|
|
||||||
[disabled]="flashFirmwareButtonDisbabled$ | async"
|
|
||||||
(change)="changeFile($event)">
|
|
||||||
</label>
|
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
} from '../../../store';
|
} from '../../../store';
|
||||||
import { UpdateFirmwareAction, UpdateFirmwareWithAction } from '../../../store/actions/device';
|
import { UpdateFirmwareAction, UpdateFirmwareWithAction } from '../../../store/actions/device';
|
||||||
import { XtermLog } from '../../../models/xterm-log';
|
import { XtermLog } from '../../../models/xterm-log';
|
||||||
|
import { UploadFileData } from '../../../models/upload-file-data';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'device-firmware',
|
selector: 'device-firmware',
|
||||||
@@ -48,19 +49,8 @@ export class DeviceFirmwareComponent implements OnDestroy {
|
|||||||
this.store.dispatch(new UpdateFirmwareAction());
|
this.store.dispatch(new UpdateFirmwareAction());
|
||||||
}
|
}
|
||||||
|
|
||||||
changeFile(event): void {
|
changeFile(data: UploadFileData): void {
|
||||||
const files = event.srcElement.files;
|
this.store.dispatch(new UpdateFirmwareWithAction(data.data));
|
||||||
|
|
||||||
if (files.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileReader = new FileReader();
|
|
||||||
fileReader.onloadend = function () {
|
|
||||||
const arrayBuffer = new Uint8Array(fileReader.result);
|
|
||||||
this.store.dispatch(new UpdateFirmwareWithAction(Array.prototype.slice.call(arrayBuffer)));
|
|
||||||
}.bind(this);
|
|
||||||
fileReader.readAsArrayBuffer(files[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
openFirmwareGitHubIssuePage(event): void {
|
openFirmwareGitHubIssuePage(event): void {
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<label class="btn btn-primary btn-file"
|
||||||
|
[class.disabled]="disabled">
|
||||||
|
{{ label }}
|
||||||
|
<input #inputControl
|
||||||
|
type="file"
|
||||||
|
[accept]="accept"
|
||||||
|
[disabled]="disabled"
|
||||||
|
(change)="changeFile($event)">
|
||||||
|
</label>
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
|
||||||
|
|
||||||
|
import { UploadFileData } from '../../models/upload-file-data';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'file-upload',
|
||||||
|
templateUrl: './file-upload.component.html',
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush
|
||||||
|
})
|
||||||
|
export class FileUploadComponent {
|
||||||
|
@Input() label = 'Select file';
|
||||||
|
@Input() disabled: boolean;
|
||||||
|
@Input() accept: string;
|
||||||
|
|
||||||
|
@Output() fileChanged = new EventEmitter<UploadFileData>();
|
||||||
|
|
||||||
|
changeFile(event): void {
|
||||||
|
const files = event.srcElement.files;
|
||||||
|
|
||||||
|
if (files.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileReader = new FileReader();
|
||||||
|
fileReader.onloadend = function () {
|
||||||
|
const arrayBuffer = new Uint8Array(fileReader.result);
|
||||||
|
const target = event.target || event.srcElement || event.currentTarget;
|
||||||
|
target.value = null;
|
||||||
|
this.fileChanged.emit({
|
||||||
|
filename: event.srcElement.value,
|
||||||
|
data: Array.from(arrayBuffer)
|
||||||
|
});
|
||||||
|
}.bind(this);
|
||||||
|
fileReader.readAsArrayBuffer(files[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
1
packages/uhk-web/src/app/components/file-upload/index.ts
Normal file
1
packages/uhk-web/src/app/components/file-upload/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './file-upload.component';
|
||||||
@@ -107,6 +107,7 @@ import { SliderWrapperComponent } from './components/slider-wrapper/slider-wrapp
|
|||||||
import { EditableTextComponent } from './components/editable-text/editable-text.component';
|
import { EditableTextComponent } from './components/editable-text/editable-text.component';
|
||||||
import { Autofocus } from './directives/autofocus/autofocus.directive';
|
import { Autofocus } from './directives/autofocus/autofocus.directive';
|
||||||
import { UhkDeviceBootloaderNotActiveGuard } from './services/uhk-device-bootloader-not-active.guard';
|
import { UhkDeviceBootloaderNotActiveGuard } from './services/uhk-device-bootloader-not-active.guard';
|
||||||
|
import { FileUploadComponent } from './components/file-upload';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
@@ -179,7 +180,8 @@ import { UhkDeviceBootloaderNotActiveGuard } from './services/uhk-device-bootloa
|
|||||||
EditableTextComponent,
|
EditableTextComponent,
|
||||||
Autofocus,
|
Autofocus,
|
||||||
RestoreConfigurationComponent,
|
RestoreConfigurationComponent,
|
||||||
RecoveryModeComponent
|
RecoveryModeComponent,
|
||||||
|
FileUploadComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
CommonModule,
|
CommonModule,
|
||||||
|
|||||||
Reference in New Issue
Block a user