我一直在使用带有rest-client的rails上传文件,但是当我将其更改为ajax时,
会出现一些服务器错误。
表格代码
<%= form_tag({:controller => 'person',:action => 'create'},:multipart => true) do
%>
<%= file_field_tag 'upload', :class => 'upload_name' %>
<%= submit_tag "Upload", :class => "photo_up btn btn-success" %>
<%end%>
AJAX 代码
$(function(){
$(".photo_up").click(function(){
var filename = $(".upload_name").val();
$.ajax({
url: '/create', //your server side script
data: { upload: filename}, //our data
type: 'POST',
success: function (response) {
}
});
return false;
});
});
控制器代码
file_param = params[:upload]
puts "name os #{file_param}"
show_url = "............."
resource = RestClient::Resource.new show_url, :user => "admin",:password => "admin"
response = resource.post :file => file_param, :multipart => true
在这里,当我运行此代码时,出现服务错误 RestClient::InternalServerError (500 Internal Server Error):,并且我知道服务器显示空指针,在控制器中它在此行中给出错误response = resource.post :文件 => 文件参数,:multipart => 真。但是它在没有ajax的正常形式下运行。