2

I am trying to upload a file using sample code. I am not getting any compilation errors but a 406 Not Acceptable error message. What does this mean?

POST request:

Started POST "/files/upload" for 10.15.24.38 at 2012-06-22 21:18:11 -0400
Processing by FilesController#upload as HTML
  Parameters: {"utf8"=>"â", "authenticity_token"=>"wfpBi3Y8KgmqitXrh4fZ3xfun73mWOfXiTQ+J7bdfWU=", "user_id"=>"231",
"button"=>"", "upload"=>{"datafile"=>"ws_1920x1080.jpg"}}

View:

<%= form_tag files_upload_path, :remote => true, :method => :post, :multipart => true do %>
#The form has some other values too. I am keeping it short here.  
<p>
<label for="upload_file">Select File</label> :
<%= file_field 'upload', 'datafile' %>
</p>
<% end %>

Controller:

 def upload
    #The controller has some other values too, that are not related to file upload. 

    if params[:datafile]
    uploaded_io = params[:upload][:datafile]
    File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
    file.write(uploaded_io.read)
    end
    end

    if params[:user_id] 
    #do some extra stuff
    end 

    respond_to do |format|
    format.js
    end 

 end

Error:

Completed 406 Not Acceptable in 2736ms (ActiveRecord: 3.2ms)
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Majoris
  • 2,963
  • 6
  • 47
  • 81
  • there's not enough info here. Please paste the entire controller action. – x1a4 Jun 23 '12 at 01:38
  • @x1a4 - The controller is too large. And the code here is the only relevant code for uploading a file. Even if I completely remove this upload code from the controller then still I get the error `Completed 406 Not Acceptable` – Majoris Jun 23 '12 at 01:42
  • You need to post the form and the full controller action. – Maurício Linhares Jun 23 '12 at 01:47
  • Updated my question with some more code. – Majoris Jun 23 '12 at 01:53
  • All we need to see is the action, not the entire controller. What's there still isn't enough information. The action will either render a response, or redirect somewhere. That's what you need to show. – x1a4 Jun 23 '12 at 02:31
  • @x1a4 - I actually render back as ajax using javascript. – Majoris Jun 23 '12 at 02:39
  • @KapishM What are you using remotipart to upload the file since you can just directly upload a file using ajax like normal form unless your using some sort of plugin or something like it – Viren Jun 23 '12 at 09:33

3 Answers3

0

There isn't enough information to provide the answer but you're clearly missing :multipart => true in our form_for call as in:

<%= form_for @upload, :multipart => true do |f| %>
  Your form code here
<% end %>

And the not acceptable error happens because you're asking for HTML rendering but there is probably not a respond_to piece answering to HTML.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
0

You are not allowed (in direct way) to upload files through remote request. Take a look here: How can I upload files asynchronously?

Community
  • 1
  • 1
0

I guess you cant upload a file using ajax just by specifying remote => true you need some sort of plugin or something like it to do it like

remotipart , plupload , uploadify or something like it .

so if you are not using any plugin then even if you specify remote => true the form would be sent as normal HTML request instead of XMLHttpRequest

Now since in your controller you have removed

format.html part as you are assuming that it would be an ajax request but in reality it is not (HTML request)

becoz Rails cannot find the appropriate format to respond back it is reporting 406 Not Acceptable

Do your self a favor log the request.xhr? method in your controller.

something like

logger.info "******************** #{request.xhr?} *********************"

If request.xhr? return nil that specify that request is not an ajax request

Viren
  • 5,812
  • 6
  • 45
  • 98