【发布时间】:2018-06-27 11:45:14
【问题描述】:
各位,我要做的是用一个按钮来加载一个本地文件,并将文件数据传输到一个jsonObject中。
.html:
<div class="uuidLableLeft"> <b>Do your want to import your previous file (.json)?</b>
<input style="display: none" type="file" accept=".json"
(change)="onFileLoad($event)" #fileinput>
<button type="button" (click)="fileinput.click()"> load </button>
</div>
...
...
<div>
<textarea class="form-control" placeholder="" [(ngModel)]="cUser.vision" ></textarea>
</div>
.ts:
onFileLoad (event) {
const f = event.target.files[0];
const reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
try {
const json = JSON.parse(e.target.result);
const resSTR = JSON.stringify(json);
this.cUser = JSON.parse(resSTR);
console.log('... uuid of cUser: ', this.cUser.id);
} catch (ex) {
alert('exception when trying to parse json = ' + ex);
}
};
})(f);
reader.readAsText(f);
}
问题是:
- this.cUser 未将更改传递给 html
- 更改为“未定义”消息
如果我通过提供静态路径来加载文件,它可以工作,
this.http.request('assets/Your_filename.json')
但是如何通过导入按钮来实现呢?
或者还有其他不使用文件阅读器的方法吗?非常感谢!!
【问题讨论】: