5

I want to download a file that it's API service created in php7. when I receive service output from API service, the file doesn't download. But if I see service output in developer tools of browser it's correct. What is wrong in my app?

To get API service in angular I use HttpClient of angular2 & get method. after subscribe the http service result is not empty but not download file. How can I use stream downloading in angular2?

downloadAttachment(filename: string){
   return this.http.get( this.url +"/"+ filename)
            .catch(this.handleError);
}
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
k m
  • 95
  • 1
  • 5

3 Answers3

2

you can use FileSave package exist in npm:

npm install --save file-saver
..
import { saveAs } from 'file-saver/FileSaver';

then in your method in subscribe after get result use like bellow

saveAs(result, filename);
hamid_reza hobab
  • 925
  • 9
  • 21
1

If it is a Blob response, you will have to do something like:

 const header = {responseType: 'blob', observe: 'response'};
 this.http.get( this.url +"/"+ filename, headers)
          .map(res => ({content: res.body, fileName: res.headers.get('content-filename')}));

And then you can use may be file-saver to make the blob as downloadable file.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
1

    let headers = new Headers({ name: "X-Requested-With", value: "XMLHttpRequest" });
    let url = `https://api.cloudinary.com/v1_1/${cloudinary_config.cloud_name}/image/upload`;
    let form: FormData = new FormData();
    form.append("upload_preset", cloudinary_config.upload_preset);
    form.append("context", `photo=${this.title}`);
    form.append("folder", window.location.hostname);
    form.append("tags", "myphotoalbum");
    form.append("file", file);

    this.http.post(url, form).subscribe(
      (data: any) => {
        console.log("sonuc", data);
        this.success(data.toString());
        this.imageUrl.emit(JSON.stringify(data));
      },
      error => {
        this.warn("hata oluştu");
      },
      () => { // "sonuc"
      }

    );
zinderud
  • 27
  • 2