Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Created July 29, 2011 02:51
Show Gist options
  • Save earlonrails/1113038 to your computer and use it in GitHub Desktop.
Save earlonrails/1113038 to your computer and use it in GitHub Desktop.
send a jpeg file over https using ruby. Also including rails controller receiving method.
def receive_images_contr_method
image_path = "tmp/my_img#{params[:id_my_img]}.jpg"
content_length = request.content_length
my_read = request.body.read(content_length)
my_base64 = Base64.decode64(my_read)
image = open(image_path, "w")
image.write(my_base64)
image.close
end
require 'uri'
require 'base64'
require 'net/http'
require 'net/https'
img_file = "my_img.jpg"
img_url = "www.my_url.com/img/receive_images_contr_method?id_my_img=1"
url = URI.parse(img_url)
file = open(img_file)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == 'https')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(url.path + '?' + url.query)
request.body = Base64.encode64(file.read)
request["Content-Type"] = "text/plain"
response = http.request(request)
response.code
response.body
file.close
@NoSkillGuy
Copy link

small improvement

image_path = File.join('tmp',"my_img#{params[:id_my_img]}.jpg")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment