django - 将 pdf 从 Django 后端传递到 Angular 前端
问题描述
我无法为我的案例提供任何类似问题的解决方案。我想使用 django 从文件系统加载 pdf 并通过 API 调用将其返回给 Angular,以便显示它。我的 Django 代码差不多:
class LoadPdfViewSet(views.APIView):
def get(self, request):
# some code here here
response = FileResponse(open(path_to_pdf, 'rb').read())
response.headers = {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment;filename="report.pdf"',
}
response.as_attachment = True
return response
而在 Angular 方面,我有一项服务可以做到这一点:
export class LoadPdfService {
constructor(
private http: HttpClient
) {}
getPdf(): Observable<Blob> {
const params = new HttpParams({
fromObject: {
responsetype: 'arraybuffer'
// other stuff here
}
})
return self.http.get<Blob>(loadpdf_api_url, {params}).pipe(catchError(self.myErrorHandler))
}
}
以及一个尝试像这样打开 pdf 的组件:
export class MyComponent {
constructor(
public loadPdfService: LoadPdfService
) {}
download_pdf() {
let call = self.loadPdfService.getPdf();
call.subscribe( (response:Blob) => {
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
window.navigator.msSaveOrOpenBlob(blob, "report.pdf");
} else {
let pdfUrl = URL.createObjectURL(blob)
window.open(pdfUrl, '_blank')
URL.revokeObjectURL(pdfUrl);
}
}
}
}
但什么也没发生。我还尝试在 django 端使用不同的响应和直通渲染器,以及Observable<Response>
像这样的.then()
回调
response.arrayBuffer().then(buffer => new Blob([buffer], {type: 'application/pdf'}))
在角度方面。有时我设法打开新窗口/标签,但无法显示 pdf。
解决方案
我终于弄明白了。在python部分,read()
可以毫无问题地删除。问题在于服务响应类型和响应映射:
getPdf(): Observable<Blob> {
const options = {
params: new HttpParams({
fromObject: {
// my own parameters here
}
}),
responseType: 'blob' as 'json'
};
return this.http.get(this.url, options).pipe(
map(response => response as Blob),
catchError(this.myErrorHandler))
}
推荐阅读
- php - Laravel 原始表达式查询本地化 (v 5.6)
- android - 找不到符号方法 setUser_list_user_view(User_List_UserViewModel) 虽然它在绑定类中可用
- ios - 如何在 iOS 中从该静态库构建胖库时从 libWebRTC.a 静态库中删除 x86_64 架构?
- sql - 在分区的范围内查找缺失值,然后转发填充值?
- arrays - Angular6/Typescript - 将对象添加到另一个数组的数组中
- sql - SQL Join 3个表并获取最新的购买日期
- maple - 如何在 MAPLE 中使用其他工作表中编写的函数
- scala - Spray Json:将泛型类型转换为 JsonFormat
- google-apps-script - Google 电子表格上的时间驱动条件更新
- azure-devops - 为(5 个用户)构建和发布包 vsts 免费版