# Ajax - problems with uploading files

**URL:** <https://www.ruby-forum.com/t/ajax-problems-with-uploading-files/103331>\
**Category:** Rails\
**Created:** [May 4, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/ajax-problems-with-uploading-files/103331 "2007-05-04T12:23:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Daniello](https://www.ruby-forum.com/u/Daniello)\
**Post date:** [May 4, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/ajax-problems-with-uploading-files/103331/1 "2007-05-04T12:23:07Z")

</div>

This is my working! model view and controller (without AJAX):

Model:  
attachment.rb

class Attachment \< ActiveRecord::Base  
belongs\_to :task  
belongs\_to :user  
validates\_presence\_of :comment

def uploaded\_file=(file\_field)  
self.name = base\_part\_of(file\_field.original\_filename)  
self.content\_type = file\_field.content\_type.chomp  
self.data = file\_field.read  
end

def base\_part\_of(file\_name)  
File.basename(file\_name).gsub(/[^\w.\_-]/, ‘’ )  
end  
end

View:

\<% form\_for(:attachment, :url =\> {:controller =\>  
‘attachment’, :action =\> ‘save’, :task\_id =\> params[:id]}, :html =\>  
{ :multipart =\> true }) do |form| %\>  
Upload your file:  
  
  
\<%= form.file\_field(:uploaded\_file) %\>  
  
Comment:  
  
  
\<%= form.text\_field(:comment) %\>  
  
  
\<%= submit\_tag(“Upload file” ) %\>  
\<% end %\>

Controller#save:

def save  
params[:attachment][:user\_id] = current\_user.id  
params[:attachment][:task\_id] = params[:task\_id]  
@attach = Attachment.new(params[:attachment])  
if @attach.save  
flash[:notice] = “New attachment added”  
else  
flash[:notice] = “There were problems”  
end  
redirect\_to(:controller =\> ‘tasks’ , :action =\> ‘edit’ , :id =\>  
params[:task\_id])  
end

And this is not working! controller and view with AJAX (model is the  
same):

Controller#add\_new:

def add\_new  
@task = Task.find\_by\_id(params[:id])  
params[:attachment][:user\_id] = current\_user.id  
params[:attachment][:task\_id] = params[:task\_id]  
@attach = Attachment.new(params[:attachment])  
if @attach.save  
@attach = @task.attachments  
end  
render :partial =\> “./tasks/ajax\_attachments”  
end

View:

\<% remote\_form\_for(:attachment,  
:before =\> “Element.show(‘spinner’)”,  
:success =\> “Element.hide(‘spinner’)”,  
:url =\> { :controller =\> ‘attachment’,  
:action =\> ‘add\_new’,  
:task\_id =\> @task },  
:update =\> ‘atte’,  
:html =\> { :multipart =\> true }) do |form| %\>  
Upload your file:  
  
  
\<%= form.file\_field(:uploaded\_file) %\>  
  
Comment:  
  
  
\<%= form.text\_field(:comment) %\>  
  
  
\<%= submit\_tag(“Upload file” ) %\>  
\<% end %\>

When I use ajax this code doesn’t work and error appears like that:

undefined method `original\_filename’ for “E:\article.txt”:String

and ApplicationTrace

#{RAILS\_ROOT}/app/models/attachment.rb:7:in `uploaded_file=' #{RAILS_ROOT}/app/controllers/attachment_controller.rb:25:in`new’  
#{RAILS\_ROOT}/app/controllers/attachment\_controller.rb:25:in `add_new' -e:4:in`load’  
-e:4

How can I fixed It???

---

<div class="post-metadata">

**Author:** ![Daniello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Daniello](https://www.ruby-forum.com/u/Daniello)\
**Post date:** [May 4, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/ajax-problems-with-uploading-files/103331/2 "2007-05-04T12:46:14Z")

</div>

On 04 May 2007, at 12:22, Daniello wrote:

> \<%= form.file\_field(:uploaded\_file) %\>
> 
> and ApplicationTrace
> 
> #{RAILS\_ROOT}/app/models/attachment.rb:7:in `uploaded_file=' #{RAILS_ROOT}/app/controllers/attachment_controller.rb:25:in `new’  
> #{RAILS\_ROOT}/app/controllers/attachment\_controller.rb:25:in `add_new' -e:4:in `load’  
> -e:4
> 
> How can I fixed It???

This has come up so many times on this list already, but here we go:

JavaScript security doesn’t allow access to the local filesystem, so  
you can’t upload files using AJAX. You can however let the user get  
the impression the files are uploaded through AJAX.

A few possibilities:  
Â• Using a hidden iframe to post the file to, the iframe will handle  
the upload and it’s a normal HTTP request. If you want upload  
progress, you’ll have to use a periodical updater to poll the  
progress on the server. There are a few blog posts (I believe one was  
posted just a few days ago) showing you how to do it this way.  
Â• Use SWFUpload ([http://swfupload.mammon.se/](http://swfupload.mammon.se/)), my personal  
preference, even though the user will need Flash. The advantage is  
that you can do all of the filtering (filetypes, max size, Â…)  
clientside and the upload progress is handled by the Flash file (it  
monitors the progress of the stream). One of the apps I’m using it in  
can be found over here (make sure you upload a picture \> 800 KB if  
you want to see the upload progress): [http://demo.placid.be/albums/](http://demo.placid.be/albums/)  
2;manage

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Daniello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Daniello](https://www.ruby-forum.com/u/Daniello)\
**Post date:** [June 13, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/ajax-problems-with-uploading-files/103331/3 "2007-06-13T20:43:09Z")

</div>

Does Javascript allow download from the server to the local  
filesystem ?  
I ask that because i am trying to initiate a file download through a  
link\_to\_function method which contains an Ajax.Request … Here it is :

\<%= link\_to\_function(“Ajax Download Pic”,  
"new Ajax.Request(’/main/dl\_photos/#{personne.id}?index=1’, {});  
") %\>

But the file does not download, even if the terminal show a 200 status  
request with a sending data"XXX.jpg", by monitoring with a TCP monitor  
the link above and a non Ajax one like :  
\<%= link\_to “Download Pic”, :action =\> “dl\_photos”, :id =\>  
personne.id, :index=\> personne.images.at(0).id %\> i get the same  
response to both request, but the later works … any idea ??
