Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何使用角度';s2&x2B@角度/http模块?_Angular_Rxjs - Fatal编程技术网

Angular 如何使用角度';s2&x2B@角度/http模块?

Angular 如何使用角度';s2&x2B@角度/http模块?,angular,rxjs,Angular,Rxjs,我正在尝试从angular 2应用程序中提供pdf下载 此代码适用于: var reportPost = 'variable=lsdkjf'; var xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost/a2/pdf.php", true); xhr.responseType = 'blob'; xhr.setRequestHeader("Content-type", "appl

我正在尝试从angular 2应用程序中提供pdf下载

此代码适用于:

    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);
但我希望使用Angular2的内置Http客户端

一点研究:

  • 过帐时未写入的有角度的单据:
  • 不确定这是否适用,但rxjs responseType参数 要仅包含文本和json,请执行以下操作:
还有一些测试代码:

    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函数来自于此:

随着Angular2 final的发布,我们可以定义一个服务:

@Injectable()
export class AngularService {

    constructor(private http: Http) {}

    download(model: MyModel) { //get file from service
        this.http.post("http://localhost/a2/pdf.php", JSON.stringify(model), {
            method: RequestMethod.Post,
            responseType: ResponseContentType.Blob,
            headers: new Headers({'Content-Type', 'application/x-www-form-urlencoded'})
        }).subscribe(
            response => { // download file
                var blob = new Blob([response.blob()], {type: 'application/pdf'});
                var filename = 'file.pdf';
                saveAs(blob, filename);
            },
            error => {
                console.error(`Error: ${error.message}`);
            }
        );
    }
}
此服务将获取文件,然后将其提供给用户


zip文件示例:

带有
@4.3、@5
,最后我做了:

this.http.post(`${environment.server}/my/download`,
                data, 
                {responseType: 'blob', observe: 'response'})
              .map( res => ({content: res.body, 
                             fileName: res.headers.get('content-filename')}));
请看这里:


似乎blob()方法尚未实现。看见跟踪实现状态的问题可以在这里找到:您究竟是如何让“saveAs”工作的?我已经安装了模块,安装了打字,但在运行它时仍然得到“no-such-function:saveAs”。。太烦人了。。。如何将其包含在typescript代码中?(我使用webpack,还没有弄明白)我只是忽略了TS编译器的错误。它仍然编译。。。邋遢吧?谢谢!我错过了responseType:ResponseContentType.Blob。有人能把这个标记为正确答案吗?我已经找了好几年了。从'@angular/http'导入{ResponseContentType};我必须使用http.get(url,{responseType:“application/octet stream”}),因为http.get无法识别ResponseContentType类型的responseType,并且需要字符串。您不能设置除“json”之外的其他字符串,因此我不得不愚弄TS编译器。我得到了以下错误
[TS]属性“blob”在类型“blob”上不存在。
有人知道为什么吗?
return this._http.get('/api/images/' + _id, {responseType: 'blob'}).map(blob => {
  var urlCreator = window.URL;
  return this._sanitizer.bypassSecurityTrustUrl(urlCreator.createObjectURL(blob));
})