# File upload problems when moving application to new server

**URL:** <https://www.ruby-forum.com/t/file-upload-problems-when-moving-application-to-new-server/124872>\
**Category:** Rails\
**Created:** [January 3, 2008, 11:51am UTC](https://www.ruby-forum.com/t/file-upload-problems-when-moving-application-to-new-server/124872 "2008-01-03T11:51:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [January 3, 2008, 11:51am UTC](https://www.ruby-forum.com/t/file-upload-problems-when-moving-application-to-new-server/124872/1 "2008-01-03T11:51:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Eivind\_LSSSSland-An](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@Eivind\_LSSSSland-An](https://www.ruby-forum.com/u/Eivind_LSSSSland-An)\
**Post date:** [January 15, 2008, 11:27am UTC](https://www.ruby-forum.com/t/file-upload-problems-when-moving-application-to-new-server/124872/2 "2008-01-15T11:27:11Z")

</div>

This issue was fixed when upgrading from Rails 1.2.6 to 2.0.2.
