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}

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: [email protected]
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}

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: [email protected]
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: [email protected]
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}

Ezra,

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

Best regards,
Bill
----- Original Message -----
From: “Ezra Z.” [email protected]
To: [email protected]
Sent: Tuesday, January 23, 2007 10:14 PM
Subject: [Rails] Re: Small files getting corrupted on upload

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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)