how to save image in blob field using watir?

邮差的信 提交于 2019-12-04 18:09:40

Take a look at this quick example I wrote up. Essentially, I just find the image I want using an id and then get it's source. Then, I open a temp file to hold the contents of the image and finally, I open the url of that src and write it to the temp file. The benefit of using tempfile is there is no cleanup.

require 'watir-webdriver'
require 'open-uri'
require 'tempfile'

browser = Watir::Browser.new :firefox
browser.goto("http://www.reddit.com")
img = browser.image(:id, "header-img").src

tempFile = Tempfile.new('tempImage')
open(img, 'rb') do |image|
  tempFile.write(image.read)
end
tempFile.rewind
puts tempFile.read ###Make your database call here, simply save this tempFile.read as a blob 
tempFile.close
tempFile.unlink # completely deletes the temp file
browser.close

For this example, I'm just getting the reddit logo and printing the binary data out to the screen. You never specified which database you're using so I didn't want to assume, but instead of doing 'puts' you'd make your DB call there.

img=cell.image.src
image = Net::HTTP.get_response(URI.parse(img)).body
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!