# File-uploads in rails 2.0.2 + AJAX

**URL:** <https://www.ruby-forum.com/t/file-uploads-in-rails-2-0-2-ajax/128902>\
**Category:** Rails\
**Created:** [February 12, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/file-uploads-in-rails-2-0-2-ajax/128902 "2008-02-12T15:24:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@Felix\_N](https://www.ruby-forum.com/u/Felix_N)\
**Post date:** [February 12, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/file-uploads-in-rails-2-0-2-ajax/128902/1 "2008-02-12T15:24:01Z")

</div>

hi,

I am porting an app from 1.2.5 to 2.0.2 and I am having problems with  
file  
uploads. The form fields look like this:

\<%= ‘Add attachment’.t %\>: \<%= file\_field ‘attachments’, “upload#{id}”,  
:size =\> ‘40’ %\>

The old code expects params[:attachments][“upload#{i}”] to be an object  
(File?)  
with a method “original\_filename”.  
I am using ajax (and previously didn’t):

\<% form\_remote\_tag(:url =\> url\_for(:controller =\> :mex, :action =\>  
:submit\_answer, :id =\> mex\_exercise.id), :update =\>  
“answer\_#{mex\_exercise.id}”, :html =\> {:multipart =\>  
mex\_exercise.num\_attachments\_allowed \> 0}) do %\>

(num\_attachments\_allowed is \> 0 and I see the enctype-attribute:

)

Do I need to change anything because of ajax?

thanks,

–  
Felix N.

---

<div class="post-metadata">

**Author:** ![Felix\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@Felix\_N](https://www.ruby-forum.com/u/Felix_N)\
**Post date:** [February 12, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/file-uploads-in-rails-2-0-2-ajax/128902/2 "2008-02-12T15:38:51Z")

</div>

On 12 Feb 2008, at 14:22, Felix N. wrote:

> The old code expects params[:attachments][“upload#{i}”] to be an
> 
> )
> 
> Do I need to change anything because of ajax?
> 
> File uploads can’t be done over ajax (because the javascript isn’t  
> allowed to read files from your computer). There are tricks you can  
> play with iframes and so on.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![Felix\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@Felix\_N](https://www.ruby-forum.com/u/Felix_N)\
**Post date:** [February 12, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/file-uploads-in-rails-2-0-2-ajax/128902/3 "2008-02-12T17:10:04Z")

</div>

On 12 Feb 2008, at 15:22, Felix N. wrote:

> I am using ajax (and previously didn’t):  
> parameters:Form.serialize(this) + ‘&authenticity\_token=’ +  
> encodeURIComponent(‘a5de291b429a9def0d4ff822d8e278172e472a28’)});  
> return false;"\>
> 
> )
> 
> Do I need to change anything because of ajax?

Indeed as another poster has already mentioned you can’t upload files  
through ajax (javascript security).

Either use a Flash uploader like [swfupload.org](http://swfupload.org) (but if you’re not  
fairly fluent in Javascript, i would advise against it), do know that  
the new session security measures cause problems if you want the  
upload to be authenticated or search for the responds\_to\_parent  
plugin and follow the instructions.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Felix\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c57346/32.png) [@Felix\_N](https://www.ruby-forum.com/u/Felix_N)\
**Post date:** [March 10, 2008, 3:01pm UTC](https://www.ruby-forum.com/t/file-uploads-in-rails-2-0-2-ajax/128902/4 "2008-03-10T15:01:59Z")

</div>

I also use v2.0.2 and have problem with remote file upload. I followed  
the instructions from this page  
[http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment\_fu-and-responds\_to\_parent](http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent)  
and getting error  
undefined method `content_type' in vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb:260:in `uploaded\_data

anybody handled this problem in rails 2 ?

Marek
