我正在尝试将图像保存到带有sinatra-activerecord的数据库中,以便稍后返回。目前(我认为)储蓄运作正常:
Photo.create(photo:params[:image][:tempfile].read)但是当我试图返回它时,浏览器显示的不是一个图像图标,当我实际进入浏览器将要访问的url时,我会看到一堆随机字符。我怎么才能让它起作用,为什么我没有工作呢?
以下是我试图归还它的方式:
file = Tempfile.new('photo')
file.write(Photo.find(params[:id]).photo)
return file发布于 2016-07-13 14:05:28
哇,我真不敢相信你还不知道这个@thesecretmaster,很明显你应该保存提交的content-type (params[:image][:type]),然后当你return file的时候,你应该在重新处理存储的二进制图像之前设置content_type <previously saved content-type>。另外,如果您还没有这样做,您必须使用activerecord中的二进制类型存储params[:image][:tempfile].read,否则您可能会得到一些AttributteError。
https://stackoverflow.com/questions/38341100
复制相似问题