Search code examples
javascriptruby-on-railsrubybase64html2canvas

How save a base64 image as file in Rails app


i can't save a base64 image as file by controller and don't get any errors, i don't want to save in db, just in public folder.

i'm using Rails 4.2.8 and Ruby 2.1, it's a legacy app, and html2canvas to generate base64 image.

my controller def:

def save_image
  image = params[:image]
  file = "#{Rails.root}/public/dashboard.png"
  File.open(file,'wb') do |f|
    f.write Base64.decode64(image)
  end
  file.save
end

i try using a Tempfile and using now an existing file, and "image = request.body['image']", both not work.

route:

post 'save_image' => 'intranet/dashboard#save_image'

and i call the post req in html2canvas function:

html2canvas(document.getElementById('list')).then(canvas => {
        base64String = canvas.toDataURL();
        var url = '/save_image';
        $.ajax({
            type: "POST",
            url: url,
            dataType: 'text',
            data: {
                image : base64String
            }
        });
      });

don't have any alert on logs and i don't know how debug this.


Solution

  • I resolve with this:

    protect_from_forgery except: [:save_image]