【问题标题】:Read an input with viewChild使用 viewChild 读取输入
【发布时间】:2019-02-22 15:47:50
【问题描述】:
我有一个必须阅读的输入(类型文件)。我的客户必须选择 640x480 24 位 BMP 文件。我知道可以在标题中验证这一点,但我无法读取文件的数据。我可以做些什么来访问数据,以便我可以读取标题和正文?
<input type="file" #originalFile (change)="fileChosen($event)" accept=".bmp">
【问题讨论】:
标签:
angular
html
input
viewchild
【解决方案1】:
您可以使用FileReader 上传文件。为此,您可以执行以下操作:
upload.component.ts:
constructor() {
this.reader = new FileReader();
this.reader.onloadend = this.fileLoaded;
}
fileChosen($event){
const file file = event.srcElement.files[0];
// Read the file type with file.type
// Read the file size with file.size
// Read the file with:
// this.reader.readAsArrayBuffer
// this.reader.readAsText
// this.reader.readAsDataURL
}
fileLoaded() {
// You can access the uploaded file with 'this.reader.result'
}