<table>
<tr>
<td>hello</td>
<td><img src="xyz.png" width="100" height="100"></td>
</tr>
</table>
我想將這個xyz.png以blob形式保存到我的數據庫中,那麼如何將圖像保存在blob
窗體中。如何使用watir將圖像保存在blob字段中?
<table>
<tr>
<td>hello</td>
<td><img src="xyz.png" width="100" height="100"></td>
</tr>
</table>
我想將這個xyz.png以blob形式保存到我的數據庫中,那麼如何將圖像保存在blob
窗體中。如何使用watir將圖像保存在blob字段中?
img=cell.image.src
image = Net::HTTP.get_response(URI.parse(img)).body
這是爲了將圖像保存爲blob形式。 – unknownbits 2013-05-04 17:40:33
看看我寫的這個快速示例。基本上,我只是使用一個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標誌並將二進制數據打印到屏幕上。你從來沒有指定過你使用的數據庫,所以我不想假設,但是不用做'放',你可以在那裏做數據庫調用。
thanx @James Flowers – unknownbits 2013-05-04 10:29:49
我已經得到了它的另一個解決方案。 – unknownbits 2013-05-04 10:36:43
IMG = cell.image.src \t \t \t \t \t \t \t \t圖像=淨:: HTTP.get_response(URI.parse(IMG))。身體 – unknownbits 2013-05-04 10:37:25
您需要首先將它下載到本地桌面,然後使用路徑位置將其保存到數據庫。 – 2013-04-24 11:32:05
但我想直接將其保存在我的數據庫的BLOB字段中。 – unknownbits 2013-04-24 11:41:48
這不是一個真正的問題,除了您正在使用它來瀏覽您的網站並找到標題圖片的來源。之後的一切都是基本的Ruby,或者是用於處理任何你正在使用的數據庫的幫助寶石。 – 2013-04-27 06:31:47