Reputation: 95
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.
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.
Is it possible to send the file to an Angular2.0 route and have the contents sent back to the current component?
Upvotes: 3
Views: 2789
Reputation: 95
Thanks to @zero298's comment I did some more digging into the File API and came up with a solution!
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);
};
}
Just took some digging into the ng2-file-upload code to find how to get the File object out of it.
Upvotes: 4