Ajax - problems with uploading files

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:   <br />   <%= form.file_field(:uploaded_file) %><br/>   Comment:   <br />   <%= form.text_field(:comment) %>   <br />   <%= 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:   <br />   <%= form.file_field(:uploaded_file) %><br/>   Comment:   <br />   <%= form.text_field(:comment) %>   <br />   <%= 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???

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/), 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/2;manage

Best regards

Peter De Berdt

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 ??