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); } 
0

3 Answers 3

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); 
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

thanks, but responseType is a property of HttpOption not HttpHeader
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" } );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.