Image corrupted on upload


#1

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


#2

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


#3

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


#4

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) }