admin管理员组文章数量:1326461
Hi I am new to JavaScript and Angular 2.
I want to read a text file from local machine and show the content on page. I have the file in assets folder. How can I read it using typescript?
Thanks in advance :)
I tried passing file path as "e"
but the error i am getting is on files[0]
. If I remove files[0]
error I am getting is parameter 1
is not of type 'Blob'
.
private readSingleFile(e) {
var fileName = e.files[0];
console.log(fileName);
if (!fileName) {
return;
}
var reader = new FileReader();
reader.onload = file => {
var contents: any = file.target;
this.text = contents.result;
};
reader.readAsText(fileName);
console.log(reader.readAsText(fileName))
}
Hi I am new to JavaScript and Angular 2.
I want to read a text file from local machine and show the content on page. I have the file in assets folder. How can I read it using typescript?
Thanks in advance :)
I tried passing file path as "e"
but the error i am getting is on files[0]
. If I remove files[0]
error I am getting is parameter 1
is not of type 'Blob'
.
private readSingleFile(e) {
var fileName = e.files[0];
console.log(fileName);
if (!fileName) {
return;
}
var reader = new FileReader();
reader.onload = file => {
var contents: any = file.target;
this.text = contents.result;
};
reader.readAsText(fileName);
console.log(reader.readAsText(fileName))
}
Share
Improve this question
edited Oct 26, 2016 at 7:11
Poonam
5494 silver badges15 bronze badges
asked Oct 26, 2016 at 6:02
himanshu goyalhimanshu goyal
351 silver badge3 bronze badges
1
- 1 use e.target.files u pass $event to readSingleFile from html – Akanksha Gaur Commented Oct 26, 2016 at 6:14
2 Answers
Reset to default 4instead of e.files[0]
try
e.target.files[0]
code snippet:
var fileName = e.target.files[0];
<input type='file' (change)='readSingleFile($event)'>
Thanks everyone for the responses.
Finally i read the files using 'ng2-file-upload' and uploaded them to server also.
本文标签: javascriptangular2read a file from local machineStack Overflow
版权声明:本文标题:javascript - angular2 : read a file from local machine - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742210888a2433690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论