6

How will I save data from javascript's canvas.toDataURL to an image file in ruby?

The sample code below create the image but gives an error saying the format is not recognized or damaged

require 'base64'
chart1 = params[:chart1].split(',')
image = Base64.decode64(chart1[1])

File.open("#{Rails.root}/test.png", 'wb') do|f|
  f.write(image)
end

Thanks!

NOTE: Edited. This is now ok and working

steamboy
  • 1,162
  • 5
  • 20
  • 37
  • What value is in `chart1[1]`? Have you tried using a debugger to step through your code? – Matt Ball Apr 24 '12 at 13:31
  • 2
    why do you have to call Base64.decode64 twice? – p0rter Apr 24 '12 at 13:32
  • @p0rter nice catch. its now working. tnx – steamboy Apr 24 '12 at 15:41
  • @p0rter - If you're game to recap the solution as your own answer, I'll delete my answer. (See http://meta.stackexchange.com/questions/90263/unanswered-question-answered-in-comments for elaboration of why this is helpful.) Thanks! – DreadPirateShawn Oct 09 '13 at 16:23
  • @steamboy hey dude.plz help me with this http://stackoverflow.com/questions/21980880/sending-input-type-image-to-a-folder-on-the-server/21981131?noredirect=1#comment33310449_21981131 its same as your question – Standin.Wolf Feb 24 '14 at 07:42

1 Answers1

0

Copying the answer from the comments in order to remove this question from the "Unanswered" filter:

p0rter noticed:

why do you have to call Base64.decode64 twice?

steamboy acknowledged:

@p0rter nice catch. its now working. tnx

Community
  • 1
  • 1
DreadPirateShawn
  • 8,164
  • 4
  • 49
  • 71