Small files getting corrupted on upload


#1

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}


#2

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}


#3

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}


#4

Ezra,

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

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


#5

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)