我正在尝试使用uploadify和paperclip在rails 3.0.9上传图片
当我使用uploadify上传图像时,服务器返回I / O错误。 development.log说:
Started POST "/user/albums" for 127.0.0.1 at 2012-01-09 09:07:15 -0800
Processing by User::AlbumsController#create as JSON
Parameters: {"Filename"=>"slogon.png", "_http_accept"=>"application/javascript", "folder"=>"/user/dashboard/signup/", "user_id"=>"1", "Filedata"=>#<ActionDispatch::Http::UploadedFile:0xa20907c @original_filename="slogon.png", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"Filedata\"; filename=\"slogon.png\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/tmp/RackMultipart20120109-3021-1q66icv>>, "Upload"=>"Submit Query"}
在3毫秒内完成
这是我的控制者:
def create
newparams = coerce(params)
@album = Album.new(newparams[:album])
if @album.save
flash[:notice] = "Successfully created upload."
respond_to do |format|
format.html {redirect_to @album.user}
format.json {render :json => { :result => 'success', :upload => album_path(@album) } }
end
else
render :action => 'new'
end
end
private
def coerce(params)
if params[:album].nil?
h = Hash.new
h[:album] = Hash.new
h[:album][:user_id] = params[:user_id]
h[:album][:photo] = params[:Filedata]
h[:album][:photo].content_type = MIME::Types.type_for(h[:album[:photo].original_filename).to_s
h
else
params
end
end
这是我的上传者:
<script>
<%= javascript_include_tag "swfobject.js", "jquery.uploadify.v2.1.0.js" %>
<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function() {
jQuery('#album_photo').click(function(event){
event.preventDefault();
});
jQuery('#album_photo').uploadify({
uploader : '/uploadify/uploadify.swf',
cancelImg : '/uploadify/cancel.png',
multi : true,
auto : true,
script : '/user/albums',
onComplete : function(event, queueID, fileObj, response, data) {
var dat = eval('(' + response + ')');
jQuery.getScript(dat.upload);},
scriptData : {
'_http_accept': 'application/javascript',
'format' : 'json',
'_method': 'post',
'user_id' : '<%= @user.id %>'
}
});
jQuery('#album_photo').click(function(event){
event.preventDefault();
jQuery('#album_photo').uploadifyUpload();
});
});
</script>
<h3>Upload a Photo</h3>
<%= form_for Album.new(:user_id => @user.id), :html => {:multipart => true} do |f| %>
<%= f.hidden_field :user_id, :value => @user.id %>
<p>
<%= f.file_field :photo %> </p>
<% end %>