# Image corrupted on upload

**URL:** <https://www.ruby-forum.com/t/image-corrupted-on-upload/62232>\
**Category:** Rails\
**Created:** [June 1, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/image-corrupted-on-upload/62232 "2006-06-01T19:09:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [June 1, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/image-corrupted-on-upload/62232/1 "2006-06-01T19:09:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [June 1, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/image-corrupted-on-upload/62232/2 "2006-06-01T19:19:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [June 1, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/image-corrupted-on-upload/62232/3 "2006-06-01T19:26:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [June 1, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/image-corrupted-on-upload/62232/4 "2006-06-01T19:34:59Z")

</div>

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