# Small files getting corrupted on upload

**URL:** <https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176>\
**Category:** Rails\
**Created:** [January 24, 2007, 2:10am UTC](https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176 "2007-01-24T02:10:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [January 24, 2007, 2:10am UTC](https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176/1 "2007-01-24T02:10:07Z")

</div>

I’m having a problem with small files getting corrupted on upload.  
They’re fine when they’re created on the server, fine when they’re  
received after being downloaded, but when I upload the same files  
they’re corrupted. My upload code is included below. Maybe I misplaced  
a comma or something. The only clues I’ve got is that the corrupted  
file is larger than the original and Mongrel isn’t ‘letting go’. I have  
to shut Mongrel down to delete the file. I’d really appreciate any  
ideas/suggestions/pointers on what might be going on here cause I’m  
definitely stumped.

TIA,  
Bill

if params[:file\_to\_upload].instance\_of?(Tempfile)  
FileUtils.copy(params[:file\_to\_upload].local\_path,  
“#{RAILS\_ROOT}/public/#{@filenametouse}”)  
else  
File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“w”){|f|  
f.write(params[:file\_to\_upload].read)  
f.close}

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [January 24, 2007, 2:30am UTC](https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176/2 "2007-01-24T02:30:09Z")

</div>

One more thing that might shed some light…

The files that are getting corrupted are pdf files that are about 7KB.  
They contain a 1500 byte XML file attachment. The XML files can be  
uploaded without getting corrupted. Not the PDFs.

Thanks,  
Bill  
----- Original Message -----  
From: Bill W.  
To: removed\_email\_address@domain.invalid  
Sent: Tuesday, January 23, 2007 7:08 PM  
Subject: [Rails] Small files getting corrupted on upload

I’m having a problem with small files getting corrupted on upload.  
They’re fine when they’re created on the server, fine when they’re  
received after being downloaded, but when I upload the same files  
they’re corrupted. My upload code is included below. Maybe I misplaced  
a comma or something. The only clues I’ve got is that the corrupted  
file is larger than the original and Mongrel isn’t ‘letting go’. I have  
to shut Mongrel down to delete the file. I’d really appreciate any  
ideas/suggestions/pointers on what might be going on here cause I’m  
definitely stumped.

TIA,  
Bill

if params[:file\_to\_upload].instance\_of?(Tempfile)  
FileUtils.copy(params[:file\_to\_upload].local\_path,  
“#{RAILS\_ROOT}/public/#{@filenametouse}”)  
else  
File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“w”){|f|  
f.write(params[:file\_to\_upload].read)  
f.close}

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [January 24, 2007, 4:29am UTC](https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176/3 "2007-01-24T04:29:20Z")

</div>

Just marking the trail …

‘new’ fact: PDFs are binary.

hypothesis: it’s about handling binary files via String / Tempfile

I checked the FileUtils documentation and didn’t see anything  
specifically addressing binary vs. ascii transfers. copy\_entry does say  
it ‘preserves file types’. Hoping that’s it. Check it out first thing  
in the morning.  
----- Original Message -----  
From: Bill W.  
To: removed\_email\_address@domain.invalid  
Sent: Tuesday, January 23, 2007 7:29 PM  
Subject: [Rails] Re: Small files getting corrupted on upload

One more thing that might shed some light…

The files that are getting corrupted are pdf files that are about 7KB.  
They contain a 1500 byte XML file attachment. The XML files can be  
uploaded without getting corrupted. Not the PDFs.

Thanks,  
Bill  
----- Original Message -----  
From: Bill W.  
To: removed\_email\_address@domain.invalid  
Sent: Tuesday, January 23, 2007 7:08 PM  
Subject: [Rails] Small files getting corrupted on upload

```
I'm having a problem with small files getting corrupted on upload. 

```

They’re fine when they’re created on the server, fine when they’re  
received after being downloaded, but when I upload the same files  
they’re corrupted. My upload code is included below. Maybe I misplaced  
a comma or something. The only clues I’ve got is that the corrupted  
file is larger than the original and Mongrel isn’t ‘letting go’. I have  
to shut Mongrel down to delete the file. I’d really appreciate any  
ideas/suggestions/pointers on what might be going on here cause I’m  
definitely stumped.

```
TIA,
Bill

if params[:file_to_upload].instance_of?(Tempfile)
    FileUtils.copy(params[:file_to_upload].local_path, 

```

“#{RAILS\_ROOT}/public/#{@filenametouse}”)  
else  
File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“w”){|f|  
f.write(params[:file\_to\_upload].read)  
f.close}

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [January 24, 2007, 5:46am UTC](https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176/4 "2007-01-24T05:46:26Z")

</div>

Ezra,

Yes, I’m doing on Windows. THANK YOU!

Best regards,  
Bill  
----- Original Message -----  
From: “Ezra Z.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, January 23, 2007 10:14 PM  
Subject: [Rails] Re: Small files getting corrupted on upload

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [January 24, 2007, 5:16am UTC](https://www.ruby-forum.com/t/small-files-getting-corrupted-on-upload/83176/5 "2007-01-24T05:16:24Z")

</div>

Bill-

Are you doing this on windows? If so you need to set the binary flag  
when you open a file for writing.

File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“wb”){|f|

f.write(params[:file\_to\_upload].read)

f.close}

note the “wb” as the second arg to File.open

-Ezra

On Jan 23, 2007, at 7:28 PM, Bill W. wrote:

> ----- Original Message -----
> 
> received after being downloaded, but when I upload the same files  
> if params[:file\_to\_upload].instance\_of?(Tempfile)  
> FileUtils.copy(params[:file\_to\_upload].local\_path, “#  
> {RAILS\_ROOT}/public/#{@filenametouse}”)  
> else  
> File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“w”){|f|
> 
> f.write(params[:file\_to\_upload].read)
> 
> f.close}
> 
> >

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)
