I''m trying to display a generated .png plot via send_data(), but it results in a server error "invalid byte sequence in US-ASCII" error. I''m pretty sure this means that I need to specify the encoding somewhere, but I haven''t been able to figure out where or how to do so. Some particulars: I''m running gnuplot through a pipe to generate a plot in .png format. (I''ve tested the code by writing its output to a file and displaying it -- it works.) My Report model receives the .png image as a string: class Report << ActiveRecord::Base def imagedata image = GnuPlot(@gnuplot_commands) end end My Controller uses send_data() to serve up a page: class ReportsController < ApplicationController def plot report = Report.find(params[:id]) send_data(report.imagedata, :type => ''image/gif'') end end [FWIW, without really understanding it, I also tried added an :encoding => ''utf8'' to the send_data call -- no change.] Finally, my View presents the result: <img src="<%= url_for(:controller => ''reports'', :action => ''plot'') -%>" /> However, the send_data() call results in a server error: ArgumentError (invalid byte sequence in US-ASCII): <internal:prelude>:8:in `synchronize'' .../Ruby/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'' So the question: Where do I specify the encoding? And what encoding is appropriate for a .png binary? BTW: "rails --version" => Rails 2.3.5 "ruby --version" => ruby 1.9.1p376 "mysqul --vesion" => Ver 14.14 Distrib 5.1.36 OS = Mac OS X 10.5.8 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I dont know if it''s related to the actual error message you are getting, but I believe your send_data call should look more like this: send_data(report.imagedata, :disposition => ''inline'', :type => ''image/ png'', :filename => "graph.png") You are setting it to a GIF, and not setting the disposition to inline. On Apr 30, 1:27 am, Fearless Fool <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m trying to display a generated .png plot via send_data(), but it > results in a server error "invalid byte sequence in US-ASCII" error. > I''m pretty sure this means that I need to specify the encoding > somewhere, but I haven''t been able to figure out where or how to do so. > > Some particulars: > > I''m running gnuplot through a pipe to generate a plot in .png format. > (I''ve tested the code by writing its output to a file and displaying it > -- it works.) My Report model receives the .png image as a string: > > class Report << ActiveRecord::Base > def imagedata > image = GnuPlot(@gnuplot_commands) > end > end > > My Controller uses send_data() to serve up a page: > > class ReportsController < ApplicationController > def plot > report = Report.find(params[:id]) > send_data(report.imagedata, :type => ''image/gif'') > end > end > > [FWIW, without really understanding it, I also tried added an :encoding > => ''utf8'' to the send_data call -- no change.] Finally, my View presents > the result: > > <img src="<%= url_for(:controller => ''reports'', :action => ''plot'') > -%>" /> > > However, the send_data() call results in a server error: > > ArgumentError (invalid byte sequence in US-ASCII): > <internal:prelude>:8:in `synchronize'' > .../Ruby/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'' > > So the question: Where do I specify the encoding? And what encoding is > appropriate for a .png binary? > > BTW: > "rails --version" => Rails 2.3.5 > "ruby --version" => ruby 1.9.1p376 > "mysqul --vesion" => Ver 14.14 Distrib 5.1.36 > OS = Mac OS X 10.5.8 > -- > Posted viahttp://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sharagoz wrote:> I dont know if it''s related to the actual error message you are > getting, but I believe your send_data call should look more like this: > send_data(report.imagedata, :disposition => ''inline'', :type => ''image/ > png'', :filename => "graph.png") > You are setting it to a GIF, and not setting the disposition to > inline.Sharagoz: Thanks -- * I''d already set the disposition to inline (forgot to mention that, thanks). * I tried setting the image type to image/png -- no change. (I''d thought that png was a subset of gif, but I know now the acronym means "Png''s Not Gif" :) * This *may* be a Ruby 1.9.1 bug -- see https://rails.lighthouseapp.com/projects/8994/tickets/3941 * I now understand the :filename suggestion - thanks. Anyway, still broken. For now, I''ve worked around it using send_file instead. - ff -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.