Forum: Ruby on Rails Image corrupted on upload

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Joe C. (Guest)
on 2006-06-01 21:09
Hi, I'm using the below method to copy/upload a file.  The outcome is an
image with the proper dimensions but looks garbled and pixelated.  I'm
baffled as to what's going on.

What's even weirder is this same function with the same image works for
my colleague.  He is using OS/10 and I am using Windows Media Center.

Any ideas?

#
#   destination is the pathname of the target
#   file is the source file
#
1   def upload_file(file, destination)
2     File.open(destination, "w") { |f| f.write(file.read) }
3   end
Matthew W. (Guest)
on 2006-06-01 21:19
(Received via mailing list)
I believe that you need under windows to specify that it is a binary
file;
that's been my experience with other programming languages (C and the
like) -- Unixen treat files as binary by default and windows as text by
default.  Given that the same code works on OSX and not on windows, I
believe that is the culprit.

Hope that helps,
Matt
Joe C. (Guest)
on 2006-06-01 21:26
Matthew W. wrote:
> I believe that you need under windows to specify that it is a binary
> file;
> that's been my experience with other programming languages (C and the
> like) -- Unixen treat files as binary by default and windows as text by
> default.  Given that the same code works on OSX and not on windows, I
> believe that is the culprit.
>
> Hope that helps,
> Matt

Hey thanks for the suggestion, how would I specify that file to a binary
in Ruby?

I found that the before and after file sizes are slightly different:
before: 71,027 bytes
after: 71,287 bytes
Joe C. (Guest)
on 2006-06-01 21:34
Matthew W. wrote:
> I believe that you need under windows to specify that it is a binary
> file;
> that's been my experience with other programming languages (C and the
> like) -- Unixen treat files as binary by default and windows as text by
> default.  Given that the same code works on OSX and not on windows, I
> believe that is the culprit.
>
> Hope that helps,
> Matt

Found it, thanks that's exactly what it is!

Changed my filewriter and now all is well!

File.open(destination, "wb") { |f| f.write(file.read) }
This topic is locked and can not be replied to.