I’m having some problems with file upload on my rails application.
Everything works as expecteded, except file upload. I can upload some
files (such as Word-documents), but others (such as InDesign-
documents) results in an error (type 500). I suspect this has
something to do with mime types (?), but don’t know where to start
fixing the problem. When getting an error, the application spits out
the contents of the file I’m trying to upload to the log file:
Processing DocumentController#upload (for xx.xx.xx.xx at 2007-12-21
14:45:48) [POST]
Session ID: (…)
Parameters: {“commit”=>“Upload”, “action”=>“upload”, “id”=>“111”,
“controller”=>“document”, “myfile”=>{“myfile”=>"…a lot to data
here …"}}
NoMethodError (undefined method `read’ for #String:0x237fb20):
I am currently moving the application to a new server, and the
everything worked OK on the old one. It is the new server that has
problems. Both machines uses the same rails application and
configuration files, and are having the following configuration:
Working server:
PowerMac (Quad Intel), running Mac OS X Tiger
Rails 1.2.3
Nginx web server
mongrel cluster with 3 processes
New server (with problem)
PoweMac G5, running Mac OS X Leopard
Rails 1.2.6 (could not install 1.2.3)
Nginx web server
mongrel cluster with 3 processes
The relevant code:
=== CONTROLLER ===
def upload
Get request => show the form
if request.get?
@myfile = Myfile.new
else
# Do the upload
@myfile = Myfile.new(params[:myfile])
# ... (Setting Myfile data here)
# Check if filename exists
if not Myfile.find_by_filename_and_folder_id(@myfile.filename,
folder_id).blank?
@myfile.filename = @myfile.filename + ’ (’ +
Time.now.strftime(’%Y%m%d%H%M%S’) + ‘)’
end
if @myfile.save
redirect_to(:controller => 'project', :action => 'show', :id =>
project_id, :folder_id => params[:folder_id])
end
end
end
==== VIEW ====
File upload
<% form_tag({:action => ‘upload’, :folder_id => controller.folder_id},
{:multipart => true}) do -%>
<%= error_messages_for(‘myfile’) %>
Choose file:
<%= file_field(‘myfile’, ‘myfile’) %>
<%= submit_tag ‘Upload’ %>
<% end -%>
==== MODEL ====
Save file to folder
def myfile=(myfile_field)
if myfile_field and myfile_field.length > 0
@date_time_created = Time.now.to_f
if File.open("#{RAILS_ROOT}/uploads/#{@date_time_created}",
‘wb’) { |f| f.write(myfile_field.read) } # only save in the database
if this succeeds
self.filename =
Myfile.base_part_of(myfile_field.original_filename)
filesize = (myfile_field.length / 1000).to_i
if filesize == 0
self.filesize = 1
else
self.filesize = filesize
end
end
end
end
I’m grateful for any help!