# Send\_data =\> invalid byte sequence in US-ASCII

**URL:** <https://www.ruby-forum.com/t/send-data-invalid-byte-sequence-in-us-ascii/187831>\
**Category:** Rails\
**Created:** [April 30, 2010, 1:27am UTC](https://www.ruby-forum.com/t/send-data-invalid-byte-sequence-in-us-ascii/187831 "2010-04-30T01:27:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 30, 2010, 1:27am UTC](https://www.ruby-forum.com/t/send-data-invalid-byte-sequence-in-us-ascii/187831/1 "2010-04-30T01:27:12Z")

</div>

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:

![]()

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

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 30, 2010, 10:28am UTC](https://www.ruby-forum.com/t/send-data-invalid-byte-sequence-in-us-ascii/187831/2 "2010-04-30T10:28:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fearless\_fool](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5e9695/32.png) [@fearless\_fool](https://www.ruby-forum.com/u/fearless_fool)\
**Post date:** [April 30, 2010, 5:22pm UTC](https://www.ruby-forum.com/t/send-data-invalid-byte-sequence-in-us-ascii/187831/3 "2010-04-30T17:22:49Z")

</div>

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  
[#3941 "invalid byte sequence in US-ASCII" error for UTF-8 localized messages - Ruby on Rails - rails](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
