Angular 项目中所遇到的知识点
1. angualr 用post请求二进制文件(PDF)的接口服务
// 文件预览
previewHelpFile(fileName:string): Observable<HttpResponse<ArrayBuffer>> {
const url = `接口地址`;
return this._http.post(url, {}, { responseType: 'arraybuffer', observe: 'response' })
.pipe(
map(res => res)
)
}
2. angular 处理后端返回的二进制流(PDF),并在页面上预览显示
// Ionic2和TypeScript中对外部url资源链接做了安全限制,所以得使用这个类处理地址
import { DomSanitizer } from '@angular/platform-browser';
// 获取预览文件
public getPreviewFile(fileName): void {
this._getHelpFile.previewHelpFile(fileName).subscribe(res => {
let blob = new Blob([res.body], {type: 'application/pdf'})
let url = window.URL.createObjectURL(blob)
this.myfiles = this._domSanitizer.bypassSecurityTrustResourceUrl(url)
})
}