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.
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-06-01 19: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
C3a0d479bd322818d766a7e869c6c5b2?d=identicon&s=25 Matthew Williams (Guest)
on 2006-06-01 19: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
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-06-01 19:26
Matthew Williams 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
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-06-01 19:34
Matthew Williams 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.