Ruby on Rails 上传照片到数据库,在从数据库中找出发送给其他服务器

接收图片并存至数据库

newrequest = Request.create
newrequest.picture = params[:picture].read
newrequest.save

发送图片给其他服务器

clnt = HTTPClient.new
uri = SERVER_URI + "push_request"
@res = clnt.post(uri, {request_id: newrequest.id, picture: get_picture(newrequest.id), text: params[:text]})

def get_picture (request_id)
    request = Request.find (request_id)
    send_data request.picture , :type => "image/png"
end

其他服务器端接收图片

begin
    File.open(picture_new_path, 'wb') do |file|
	file.write picture 
    end
rescue
    ###
else
    ###
end

参考链接:

https://gist.github.com/macek/610596

http://mattberther.com/2007/10/19/uploading-files-to-a-database-using-rails

http://archive.railsforum.com/viewtopic.php?id=4642

http://apidock.com/rails/ActionController/Streaming/send_data

http://stackoverflow.com/questions/1698386/please-help-me-send-a-jpg-file-using-send-data

你可能感兴趣的:(Ruby,on,Rails)