WEB前端 怎么读取TXT内容
2个回答
展开全部
遇到了前端打开文件,读取文件信息的功能,分享一下:
<input type="file" (change)="openFile($event)" placeholder="Open file..." />
filecontent: any; //放置文件内容
filesToUpload: Array<File> = [];
getContent() {
this.readFile(this.filesToUpload).then((result) => {
this.filecontent = result;
}, (error) => {
console.error(error);
});
}
openFile(fileInput: any) {
this.filesToUpload = <Array<File>>fileInput.target.files;
this.getContent();
}
readFile(files: Array<File>) {
return new Promise((resolve, reject) => {
var fileReader = new FileReader();
fileReader.onload = function (e) {
resolve(fileReader.result);
return;
};
fileReader.readAsText(files[0]);
});
}
<input type="file" (change)="openFile($event)" placeholder="Open file..." />
filecontent: any; //放置文件内容
filesToUpload: Array<File> = [];
getContent() {
this.readFile(this.filesToUpload).then((result) => {
this.filecontent = result;
}, (error) => {
console.error(error);
});
}
openFile(fileInput: any) {
this.filesToUpload = <Array<File>>fileInput.target.files;
this.getContent();
}
readFile(files: Array<File>) {
return new Promise((resolve, reject) => {
var fileReader = new FileReader();
fileReader.onload = function (e) {
resolve(fileReader.result);
return;
};
fileReader.readAsText(files[0]);
});
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询