簡體   English   中英

如何使用watir將圖像保存在Blob字段中?

[英]how to save image in blob field using watir?

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

我想將此by形式的xyz.png保存到我的數據庫中,所以我如何以blob形式保存圖像。

看看我寫的這個快速示例。 本質上,我只是使用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徽標並將二進制數據打印到屏幕上。 您從未指定要使用的數據庫,所以我不想假設它,但是除了執行“ puts”操作外,您還可以在此處進行數據庫調用。

img=cell.image.src
image = Net::HTTP.get_response(URI.parse(img)).body

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM