|
|
|
@ -38,6 +38,7 @@ import { UtilsService } from '@core/services/utils.service'; |
|
|
|
import { DialogService } from '@core/services/dialog.service'; |
|
|
|
import { FileSizePipe } from '@shared/pipe/file-size.pipe'; |
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
|
|
|
import { bytesToString } from '@core/utils'; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'tb-file-input', |
|
|
|
@ -216,34 +217,39 @@ export class FileInputComponent extends PageComponent implements AfterViewInit, |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
const reader = new FileReader(); |
|
|
|
reader.onload = () => { |
|
|
|
let fileName = null; |
|
|
|
let fileContent = null; |
|
|
|
let mediaType = null; |
|
|
|
if (reader.readyState === reader.DONE) { |
|
|
|
fileContent = reader.result; |
|
|
|
if (fileContent && fileContent.length > 0) { |
|
|
|
if (this.contentConvertFunction) { |
|
|
|
fileContent = this.contentConvertFunction(fileContent); |
|
|
|
} |
|
|
|
fileName = fileContent ? file.name : null; |
|
|
|
mediaType = file?.file?.type || null; |
|
|
|
} |
|
|
|
} |
|
|
|
resolve({fileContent, fileName, files: null, mediaType}); |
|
|
|
}; |
|
|
|
reader.onerror = () => { |
|
|
|
resolve({fileContent: null, fileName: null, files: null, mediaType: null}); |
|
|
|
}; |
|
|
|
const onReadError = () => resolve({fileContent: null, fileName: null, files: null, mediaType: null}); |
|
|
|
|
|
|
|
if (this.readAsBinary) { |
|
|
|
const reader = new FileReader(); |
|
|
|
reader.onload = () => { |
|
|
|
const content = reader.readyState === reader.DONE ? reader.result : null; |
|
|
|
resolve(this.buildResult(file, content)); |
|
|
|
}; |
|
|
|
reader.onerror = onReadError; |
|
|
|
reader.readAsBinaryString(file.file); |
|
|
|
} else { |
|
|
|
reader.readAsText(file.file); |
|
|
|
file.file.arrayBuffer().then( |
|
|
|
buf => resolve(this.buildResult(file, bytesToString(new Uint8Array(buf)))), |
|
|
|
onReadError |
|
|
|
); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private buildResult(file: flowjs.FlowFile, content: string | ArrayBuffer): any { |
|
|
|
let fileContent: any = content; |
|
|
|
let fileName = null; |
|
|
|
let mediaType = null; |
|
|
|
if (fileContent && fileContent.length > 0) { |
|
|
|
if (this.contentConvertFunction) { |
|
|
|
fileContent = this.contentConvertFunction(fileContent); |
|
|
|
} |
|
|
|
fileName = fileContent ? file.name : null; |
|
|
|
mediaType = file?.file?.type || null; |
|
|
|
} |
|
|
|
return {fileContent, fileName, files: null, mediaType}; |
|
|
|
} |
|
|
|
|
|
|
|
private checkMaxSize(file: flowjs.FlowFile): boolean { |
|
|
|
return !this.maxSizeByte || file.size <= this.maxSizeByte; |
|
|
|
} |
|
|
|
|