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
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.
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
Ezra,
Yes, I'm doing on Windows. THANK YOU!
Best regards,
Bill