[英]show pdf in browser from HTTP.get('/my_pdf')
Ηι! Ηι!
I use HTTP.get('/ehde_pdf') that through routes.rb calls a controller method that executes this code:我使用HTTP.get('/ehde_pdf')通过 routes.rb 调用执行此代码的 controller 方法:
send_data(
"#{Rails.root}/tmp/ehde_pdf.pdf",
filename: "your_custom_file_name.pdf",
type: "application/pdf"
)
which returns pdf data to the browser, but it's doesn't show up in our page as download prompt.它将 pdf 数据返回到浏览器,但它没有作为下载提示出现在我们的页面中。 I can see from eg Mozilla Developer Tools -> Network that browser gets file data and I can download it with double-click:我可以从例如 Mozilla Developer Tools -> Network 中看到浏览器获取文件数据,我可以通过双击下载它:
Is there any way that I can get file data from HTTP.get('/ehde_pdf') and show the download prompt directly?有什么方法可以从 HTTP.get('/ehde_pdf') 获取文件数据并直接显示下载提示?
Thanx!!!谢谢!!!
Correction : the controller code is更正:controller 代码为
send_file(
"#{Rails.root}/tmp/ehde_pdf.pdf",
filename: "your_custom_file_name.pdf",
type: "application/pdf"
)
You want to use a javascript blob in this case.在这种情况下,您想使用javascript blob 。 You can do something like this:你可以这样做:
(...some api call here)
.then(res => {
const blob = new Blob([res.data]);
const link = document.createElement(triggerTag);
link.href = window.url.createObjectURL(blob);
link.download = 'some_file_name';
link.click();
window.URL.revokeObjectURL(link.href);
})
.catch(....);
This way you can programmatically trigger the download when the user clicks on a button to download the pdf.这样,您可以在用户单击按钮下载 pdf 时以编程方式触发下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.