Skip to content

Instantly share code, notes, and snippets.

@nysalor
Created March 5, 2012 04:40
Show Gist options
  • Save nysalor/1976650 to your computer and use it in GitHub Desktop.
Save nysalor/1976650 to your computer and use it in GitHub Desktop.
rails3 drag'n drop upload store method
# forked from http://kray.jp/blog/rails3-fileupload/
def _store_upload(file)
if request.xhr?
if request.body.size == request.headers['CONTENT_LENGTH'].to_i
file_data = request.body.read
file_name = file
end
else
if file.instance_of?(File)
file_data = file.read
file_name = file.original_filename
end
end
raise "upload failure" unless file_data
temp_file = Tempfile.new(file_name)
temp_file << file_data.force_encoding('utf-8')
@picture = Picture.new
@picture.image = temp_file
@picture.image_file_name = file_name
@picture.user = current_user if current_user
@picture.tags << current_tag if current_tag
@picture.stories << current_story if current_story
@picture.save
temp_file.close!
@picture
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment