2012-06-23 97 views
2

我想使用示例代碼上傳文件。我沒有收到任何編譯錯誤,但是出現406 Not Acceptable錯誤消息。這是什麼意思?上傳文件收益406(不可接受)錯誤

POST請求:

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"}} 

檢視:

<%= 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 %> 

控制器:

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 

錯誤:

Completed 406 Not Acceptable in 2736ms (ActiveRecord: 3.2ms) 
+0

有沒有足夠的信息在這裏。請粘貼整個控制器操作。 – x1a4

+0

@ x1a4 - 控制器太大。這裏的代碼是上傳文件的唯一相關代碼。即使我完全從控制器中刪除了這個上傳代碼,我仍然得到錯誤'完成406不可接受' –

+0

您需要發佈表單和完整的控制器操作。 –

回答

0

沒有足夠的信息來提供答案,但你清楚我們form_for呼叫失蹤:multipart => true爲:

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

而且是因爲你無法接受的錯誤'要求HTML呈現,但可能沒有迴應HTML的respond_to

0

我想你不能只是通過指定remote => true你需要某種形式的插件或東西的喜歡它要做得像

remotipartpluploaduploadify或類似的東西上傳使用ajax文件。

,所以如果你不使用任何插件,那麼即使您指定remote => true形式將作爲普通HTML請求,而不是XMLHttpRequest

現在既然在你的控制器,你有刪除發送

format.html的一部分,因爲你認爲它會是一個ajax請求,但實際上它不是(HTML請求)

becoz Rails cann找到合適的格式來回應它正在報告406 Not Acceptable

幫助你在控制器中記錄request.xhr?方法。

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

如果request.xhr?回報指定該請求是一個Ajax請求

相關問題