2
<table>  
<tr>
<td>hello</td>
<td><img src="xyz.png" width="100" height="100"></td>
</tr>
</table>

我想将这个 xyz.png 以 blob 形式保存到我的数据库中,那么如何以blob表格形式保存图像。

4

2 回答 2

2
img=cell.image.src
image = Net::HTTP.get_response(URI.parse(img)).body
于 2013-05-04T10:37:04.970 回答
2

看看我写的这个简单的例子。本质上,我只是使用 id 找到我想要的图像,然后获取它的来源。然后,我打开一个临时文件来保存图像的内容,最后,我打开该 src 的 url 并将其写入临时文件。使用 tempfile 的好处是没有清理。

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

对于这个例子,我只是获取 reddit 徽标并将二进制数据打印到屏幕上。您从未指定您正在使用哪个数据库,所以我不想假设,但不是做“放置”,而是在那里进行数据库调用。

于 2013-04-25T16:46:53.653 回答