如何解决Rails 6:如何使用 send_data 显示二进制图像并应用样式
我在控制器操作中以二进制数据形式获取图像文件:
def show
@result = QrcodeService.call(id)
send_data @result.body,type: 'image/png',disposition: 'inline'
end
QrCodeService
只是调用外部 API 以使用 httparty gem 获取图像。
对应的视图模板定义如下:
<h2>Scan Me</h2>
<%= image_tag @result %>
问题是显示获取的图像但没有样式,-背景只是黑色:
<body style="margin: 0px; background: #0e0e0e; height: 100%" data-new-gr-c-s-check-loaded="14.991.0" data-gr-ext-installed=""><img style="-webkit-user-select: none;margin: auto;" src="http://localhost:3000/qrcode"></body>
即使我删除了对 <%= image_tag @result %>
的调用,<h2>Scan Me</h2>
也不会显示。使用 send_data
有什么问题?
似乎 method 完全替换了正文内容?我在页面上什么都没有,只有图片...
更多的是,当查看控制台中的日志时,相同的请求会命中两次以获取图像:
Started GET "/qrcode" for 127.0.0.1 at 2021-01-12 10:31:33 +0100
Processing by QrcodeController#show as HTML
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 1)
Sent data (0.5ms)
Completed 200 OK in 374ms (Views: 0.3ms | ActiveRecord: 0.0ms | Allocations: 2116)
Started GET "/qrcode" for 127.0.0.1 at 2021-01-12 10:31:33 +0100
Processing by QrcodeController#show as HTML
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 1)
Sent data (1.0ms)
Completed 200 OK in 319ms (Views: 0.8ms | ActiveRecord: 0.0ms | Allocations: 2126)
当您尝试使用导航按钮返回浏览器时,URI 字段中的 URL 会发生变化,但页面内容不会发生变化。
如何显示作为二进制数据获取的图像并保留/应用 CSS 样式?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。