send_data => invalid byte sequence in US-ASCII

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

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 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" :slight_smile:

* 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