I need to upload an image from a rails form using Ajax and convert it into a byte array to show a html preview of the image.
When I read the file, it returns me binary data which is not readable by img tag. I’m sure I’m doing something very silly and this might have an obvious solution. Here is the code snippet. Please help.
rails
tmp = File.open(params[:file_upload][:my_file].tempfile, 'rb').read
render :text => tmp
jquery
$("#item_detail_image").attr("src", "data:image/png;base64," + data.responseText);
I’m not using paperclip because I don’t have a database connection to my rails application (I’m using web services) and I’m not sure how to use paperclip without ActiveRecord
You need to Base 64 encode the data.
See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-encode64