|
|
@ -17,6 +17,7 @@ |
|
|
import { |
|
|
import { |
|
|
AfterViewInit, |
|
|
AfterViewInit, |
|
|
Component, |
|
|
Component, |
|
|
|
|
|
ElementRef, |
|
|
EventEmitter, |
|
|
EventEmitter, |
|
|
forwardRef, |
|
|
forwardRef, |
|
|
Input, |
|
|
Input, |
|
|
@ -102,17 +103,34 @@ export class FileInputComponent extends PageComponent implements AfterViewInit, |
|
|
existingFileName: string; |
|
|
existingFileName: string; |
|
|
|
|
|
|
|
|
@Input() |
|
|
@Input() |
|
|
convertToBase64 = false; |
|
|
readAsBinary = false; |
|
|
|
|
|
|
|
|
|
|
|
private multipleFileValue = false; |
|
|
|
|
|
|
|
|
|
|
|
@Input() |
|
|
|
|
|
set multipleFile(value: boolean) { |
|
|
|
|
|
this.multipleFileValue = value; |
|
|
|
|
|
if (this.flow?.flowJs) { |
|
|
|
|
|
this.updateMultipleFileMode(this.multipleFile); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
get multipleFile(): boolean { |
|
|
|
|
|
return this.multipleFileValue; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Output() |
|
|
@Output() |
|
|
fileNameChanged = new EventEmitter<string>(); |
|
|
fileNameChanged = new EventEmitter<string|string[]>(); |
|
|
|
|
|
|
|
|
fileName: string; |
|
|
fileName: string | string[]; |
|
|
fileContent: any; |
|
|
fileContent: any; |
|
|
|
|
|
|
|
|
@ViewChild('flow', {static: true}) |
|
|
@ViewChild('flow', {static: true}) |
|
|
flow: FlowDirective; |
|
|
flow: FlowDirective; |
|
|
|
|
|
|
|
|
|
|
|
@ViewChild('flowInput', {static: true}) |
|
|
|
|
|
flowInput: ElementRef; |
|
|
|
|
|
|
|
|
autoUploadSubscription: Subscription; |
|
|
autoUploadSubscription: Subscription; |
|
|
|
|
|
|
|
|
private propagateChange = null; |
|
|
private propagateChange = null; |
|
|
@ -125,34 +143,60 @@ export class FileInputComponent extends PageComponent implements AfterViewInit, |
|
|
|
|
|
|
|
|
ngAfterViewInit() { |
|
|
ngAfterViewInit() { |
|
|
this.autoUploadSubscription = this.flow.events$.subscribe(event => { |
|
|
this.autoUploadSubscription = this.flow.events$.subscribe(event => { |
|
|
if (event.type === 'fileAdded') { |
|
|
if (event.type === 'filesAdded') { |
|
|
const file = event.event[0] as flowjs.FlowFile; |
|
|
const readers = []; |
|
|
if (this.filterFile(file)) { |
|
|
(event.event[0] as flowjs.FlowFile[]).forEach(file => { |
|
|
const reader = new FileReader(); |
|
|
if (this.filterFile(file)) { |
|
|
reader.onload = (loadEvent) => { |
|
|
readers.push(this.readerAsFile(file)); |
|
|
if (typeof reader.result === 'string') { |
|
|
} |
|
|
const fileContent = this.convertToBase64 ? window.btoa(reader.result) : reader.result; |
|
|
}); |
|
|
if (fileContent && fileContent.length > 0) { |
|
|
if (readers.length) { |
|
|
if (this.contentConvertFunction) { |
|
|
Promise.all(readers).then((filesContent) => { |
|
|
this.fileContent = this.contentConvertFunction(fileContent); |
|
|
filesContent = filesContent.filter(content => content.fileContent != null); |
|
|
} else { |
|
|
if (filesContent.length === 1) { |
|
|
this.fileContent = fileContent; |
|
|
this.fileContent = filesContent[0].fileContent; |
|
|
} |
|
|
this.fileName = filesContent[0].fileName; |
|
|
if (this.fileContent) { |
|
|
this.updateModel(); |
|
|
this.fileName = file.name; |
|
|
} else if (filesContent.length > 1) { |
|
|
} else { |
|
|
this.fileContent = filesContent.map(content => content.fileContent); |
|
|
this.fileName = null; |
|
|
this.fileName = filesContent.map(content => content.fileName); |
|
|
} |
|
|
this.updateModel(); |
|
|
this.updateModel(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
if (!this.multipleFile) { |
|
|
|
|
|
this.updateMultipleFileMode(this.multipleFile); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private readerAsFile(file: flowjs.FlowFile): Promise<any> { |
|
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
|
const reader = new FileReader(); |
|
|
|
|
|
reader.onload = () => { |
|
|
|
|
|
let fileName = null; |
|
|
|
|
|
let fileContent = null; |
|
|
|
|
|
if (typeof reader.result === 'string') { |
|
|
|
|
|
fileContent = reader.result; |
|
|
|
|
|
if (fileContent && fileContent.length > 0) { |
|
|
|
|
|
if (this.contentConvertFunction) { |
|
|
|
|
|
fileContent = this.contentConvertFunction(fileContent); |
|
|
|
|
|
} |
|
|
|
|
|
if (fileContent) { |
|
|
|
|
|
fileName = file.name; |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
if (this.convertToBase64) { |
|
|
|
|
|
reader.readAsBinaryString(file.file); |
|
|
|
|
|
} else { |
|
|
|
|
|
reader.readAsText(file.file); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
resolve({fileContent, fileName}); |
|
|
|
|
|
}; |
|
|
|
|
|
reader.onerror = () => { |
|
|
|
|
|
resolve({fileContent: null, fileName: null}); |
|
|
|
|
|
}; |
|
|
|
|
|
if (this.readAsBinary) { |
|
|
|
|
|
reader.readAsBinaryString(file.file); |
|
|
|
|
|
} else { |
|
|
|
|
|
reader.readAsText(file.file); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
@ -207,4 +251,11 @@ export class FileInputComponent extends PageComponent implements AfterViewInit, |
|
|
this.fileContent = null; |
|
|
this.fileContent = null; |
|
|
this.updateModel(); |
|
|
this.updateModel(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private updateMultipleFileMode(multiple: boolean) { |
|
|
|
|
|
this.flow.flowJs.opts.singleFile = !multiple; |
|
|
|
|
|
if (!multiple) { |
|
|
|
|
|
this.flowInput.nativeElement.removeAttribute('multiple'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|