问题
I would like to know how to open a CSV file using Angular 4. I found an example on the network, but it is not exactly what I need because in that case the file is downloaded from the server. I need to open the file locally, is it possible?
I found this example:
http://blog.sodhanalibrary.com/2016/10/read-csv-data-using-angular-2.html#.WYNdCnWGNp8
Thanks
回答1:
you can load files from the client machine using Javascript FileReader object and (Angular 2+ indeed).
import { Component } from '@angular/core';
@Component({
selector: 'app',
encapsulation: ViewEncapsulation.None,
template: `<input id="preview" type="file" (change)="previewFile($event)" />`
})
export class AppComponent implements OnInit {
...
public previewImage(event) {
const reader = new FileReader();
reader.onload = (e: any) => {
console.log('csv content', e.target.result);
};
reader.readAsDataURL(event.target.files[0]);
}
}
回答2:
you can use FileReader() api.have a look at this thread : Getting Data from FileReader in Angular 2
Yes, it is possible like described in the link you provided too You implement a webservice that will provide you the local file . your angular applications makes a request to the webservice and retrieve the local file.
来源:https://stackoverflow.com/questions/45494691/angular-4-how-to-open-csv-local-file