Skip to content

Instantly share code, notes, and snippets.

@inertialbit
Created February 24, 2012 11:28
Show Gist options
  • Save inertialbit/1900321 to your computer and use it in GitHub Desktop.
Save inertialbit/1900321 to your computer and use it in GitHub Desktop.
generate base64 multipart messages
require 'base64'
cur_dir = File.dirname(File.expand_path(__FILE__))
img_dir = File.join cur_dir, 'images'
Dir.chdir cur_dir
Dir.glob("*.{jpg,png}").each do |filename|
img = File.new(filename, 'r')
encoded_data = Base64.encode64 img.read
img.close
File.open(File.join(img_dir, filename), 'w+') do |file|
file.print "-----------------------------168072824752491622650073\r\n"
file.print "Content-Disposition: form-data; name=\"photo[image]\"; filename=\"#{filename}\"\r\n"
file.print "Content-Type: image/jpeg\r\n"
file.print "Content-Transfer-Encoding: Base64\r\n"
file.print "\r\n"
file.print encoded_data
file.print "\r\n"
file.print "-----------------------------168072824752491622650073--"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment