Am playing with Angular 2, trying to create sample app to showcase myself and i've completed almost all things. Finally, am trying to implement download option. for that, i dig SO and i got this
as per that example i try to replicate it, but came to know that HTTP_PROVIDERS got deprecated. Could some one tell me what is equivalent module available currently. And guide me to get start.
Kindly pls share any working plunker for reference
so far i have below
import { Http, ResponseContentType } from '@angular/http';
export class AboutComponent implements OnInit {
//constructor() { }
constructor(private dataService: dataService,private http: Http){}
about=[];
private filePath = './coreMachineDetails/machine109902.docx' // docx in my local folder
ngOnInit(){
//some codes
}
getFile(){
return this.http.get(this.filePath,{
responseType: ResponseContentType.Blob,
}).map(res =>{
return {
filename: 'machine109902.docx',
data: res.blob()
};
}).subscribe(res => {
console.log('start download:',res);
var url = window.URL.createObjectURL(res.data);
var a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display: none');
a.href = url;
a.download = res.filename;
a.click();
window.URL.revokeObjectURL(url);
a.remove(); // remove the element
}, error => {
console.log('download error:', JSON.stringify(error));
}, () => {
console.log('Completed file download.')
});
}
}
in view
<button class="btn waves-effect waves-light btn-primary custom-btn" (click)="getFile()"><i class="fa fa-file-pdf-o"></i> Download manuals</button>