Я пытаюсь загрузить PDF файл из приложения angular 2...
этот код работает:
var reportPost = 'variable=lsdkjf';
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost/a2/pdf.php", true);
xhr.responseType = 'blob';
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {//Call a function when the state changes.
if(xhr.readyState == 4 && xhr.status == 200) {
var blob = new Blob([this.response], {type: 'application/pdf'});
saveAs(blob, "Report.pdf");
}
}
xhr.send(reportPost);
но я бы хотел использовать angular 2 встроенный клиент Http.
небольшое исследование:
- angular docs неписаные во время публикации: https://angular.io/docs/js/latest/api/http/ResponseTypes-enum.html
- не уверен, что это применимо, но параметр rxjs responseType кажется включить только текст и json: https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/operators/ajax.md
и некоторый тестовый код:
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://localhost/a2/pdf.php', reportPost, {
headers: headers
})
.retry(3)
// .map( (res:any) => res.blob() ) // errors out
.subscribe(
(dataReceived:any) => {
var blob = new Blob([dataReceived._body], {type: 'application/pdf'});
saveAs(blob, "Report.pdf");
},
(err:any) => this.logError(err),
() => console.log('Complete')
);
спасибо!
пс. функция saveAs приходит отсюда: https://github.com/eligrey/FileSaver.js