我有发送 PDF 格式的 fileResponse
的 Pyramid 代码。现在,我想编写 Ajax 代码来接收此响应并在新窗口中打开该 PDF,如下所示: window.open(filename);
这是我的代码:
filepath = ("ledgerReport.pdf")
response = FileResponse(filepath)
response.headers['Content-Disposition'] = ("attachment; filename=ledgerReport.pdf")
return response
最佳答案
这不太适合 AJAX 流程 - window.open
用于在新窗口中打开 URL,而不是用于显示 Javascript 中可用的数据(并且请不要重新发布问题)。
将其实现为新窗口的正确方法是使用 window.open
以及传递响应的 Controller 的 URL:
window.open("/pdfs/pdf_id")
.. 或者您可以在链接上使用 target=
使链接在空白窗口中打开。
如果您想在常规 UI 内(而不是在不同的窗口中)传递 PDF,请使用 iframe。
关于javascript - 如何将 Pyramid 的FileResponse接收到ajax中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639740/