<table>
<tr>
<td>hello</td>
<td><img src="xyz.png" width="100" height="100"></td>
</tr>
</table>
i want to save this xyz.png in blob form into my db, so how can i save image in blob
form.
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
来源:https://stackoverflow.com/questions/16190826/how-to-save-image-in-blob-field-using-watir