【发布时间】:2015-05-30 22:13:38
【问题描述】:
我需要将图像从文件保存到 PostgreSQL 数据库作为二进制文件。尝试这样做:
image_file = File.open("image.png", "rb") { |file| file.read }
Image.create(product_id: product_id, image: image_file)
这段代码给了我以下错误:
string contains null byte
使用回形针或类似工具,或将二进制转换为 base64 不是一种选择。
【问题讨论】:
-
数据库中列的类型是什么?检查此链接:stackoverflow.com/questions/4386030/…
-
二进制。更改列类型也不是一种选择
-
哪条线失败了?读取文件还是将其保存到数据库中?
标签: ruby-on-rails ruby image binary