1

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>
Mr. Learner
  • 978
  • 2
  • 18
  • 48
  • What's the exact error you are facing? Also, if possible, please share Module file of your `AboutComponent` componnet. – miiiii Apr 28 '18 at 08:15
  • 404 in my browser network. I've checked my path. It also correct.. And about code is what I have in my "AboutComponent". – Mr. Learner Apr 28 '18 at 08:20
  • I meant, what's in your console logs.. after all you have used so many console.log in your code.. so what does the log say?? – miiiii Apr 28 '18 at 08:48

1 Answers1

1

Since you are refering file which is in your local .It has been blocked by framework. See more in https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

kaushik.i.B
  • 85
  • 2
  • 11