angular2中的文件内容 [英] Contents of file in angular2

查看:64
本文介绍了angular2中的文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型应用程序来进行一些XML文件聚合.用户删除一些具有相同结构的XML文件,编辑一些通用标签,然后将它们以元素列表的形式返回给xml,其中包含所有需要的数据.

I'm writing a small app to do some XML file aggregation. User drops some XML files with the same structure, edits a few common tags, and then I give them back an xml with all of the needed data in a list of elements.

我正在使用ng2-file-upload进行文件删除,但是我想不出一种获取文件内容的方法.我不想将文件上传到后端,只需阅读内容并在XML上进行一些DOM操作即可.

I'm using ng2-file-upload to do the file droping, but I can't figure out a way to get the contents of the file. I don't want to upload the file to the backend, just read in the contents and do some DOM manipulation on the XMLs.

是否可以将文件发送到Angular2.0路由并将内容发送回当前组件?

Is it possible to send the file to an Angular2.0 route and have the contents sent back to the current component?

推荐答案

感谢@ zero298的评论,我进一步研究了File API并提出了解决方案!

Thanks to @zero298's comment I did some more digging into the File API and came up with a solution!

使用ng2-file-upload的TypeScript:

TypeScript using the ng2-file-upload:

uploader: FileUploader = new FileUploader({}); //Empty options to avoid having a target URL
reader: FileReader = new FileReader();

ngOnInit() {
    this.reader.onload = (ev: any) => {
        console.log(ev.target.result);
    };
    this.uploader.onAfterAddingFile = (fileItem: any) => {
        this.reader.readAsText(fileItem._file);
    };
}

只需深入研究ng2-file-upload代码,以查找如何从中获取File对象.

Just took some digging into the ng2-file-upload code to find how to get the File object out of it.

这篇关于angular2中的文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆