2012-01-29 82 views

回答

1

首先确保您的列族验证程序是BytesType。然后,您可以将您的IMG到卡桑德拉像这样:

File.open('/tmp/image.jpg', 'r') do |f| 
    img = f.read.force_encoding('ASCII-8BIT') # ensure cassandra-cql knows this is binary 
    handle.execute("insert into img (KEY, colA) values ('rowA', ?)", img); 
end 

为了把它抓回来了:

img = handle.execute("select colA from img where KEY = 'rowA'").fetch[0] 

从轨服务这一点,你需要做一个控制器的动作流的IMG使用send_datadisposition => 'inline'

class ImgController < ApplicationController 
    def img 
    row_key = params['row_key'] 
    col_name = params['col_name'] 
    img = handle.execute("select ? from img where KEY = ?", col_name, row_key).fetch[0] 
    send_data(img, :filename => 'img.jpg', :type => 'image/jpeg', :disposition => 'inline') 
    end 
end 

然后你可以使用IMAGE_TAG链接到该控制器。看看this question for more info on streaming images from a controller

相关问题