Module | Ramaze::Helper::Upload |
In: |
lib/ramaze/helper/upload.rb
|
Helper module for handling file uploads. File uploads are mostly handled by Rack, but this helper adds some conveniance methods for handling and saving the uploaded files.
@example
class MyController < Ramaze::Controller # Use upload helper helper :upload # This action will handle *all* uploaded files def handleupload1 # Iterate over uploaded files and save them in the # '/uploads/myapp' directory get_uploaded_files.each_pair do |k, v| v.save( File.join('/uploads/myapp', v.filename), :allow_overwrite => true ) if v.saved? Ramaze::Log.info( "Saved uploaded file named #{k} to #{v.path}." ) else Ramaze::Log.warn("Failed to save file named #{k}.") end end end # This action will handle uploaded files beginning with 'up' def handleupload2 # Iterate over uploaded files and save them in the # '/uploads/myapp' directory get_uploaded_files(/^up.*/).each_pair do |k, v| v.save( File.join('/uploads/myapp', v.filename), :allow_overwrite => true ) if v.saved? Ramaze::Log.info( "Saved uploaded file named #{k} to #{v.path}." ) else Ramaze::Log.warn("Failed to save file named #{k}.") end end end end
@author Lars Olsson @since 04-08-2011
This method will iterate through all request parameters and convert those parameters which represents uploaded files to Ramaze::Helper::Upload::UploadedFile objects. The matched parameters will then be removed from the request parameter hash.
Use this method if you want to decide whether to handle file uploads in your action at runtime. For automatic handling, use Ramaze::Helper::Upload::ClassMethods#handle_all_uploads or Ramaze::Helper::Upload::ClassMethods#handle_uploads_for instead.
@author Lars Olsson @since 04-08-2011 @param [Regexp] pattern If set, only those request parameters which
has a name matching the Regexp will be checked for file uploads.
@return [Array] The uploaded files. @see Ramaze::Helper::Upload::ClassMethods#handle_all_uploads @see Ramaze::Helper::Upload::ClassMethods#handle_uploads_for
Returns list of currently handled file uploads.
Both single and array parameters are supported. If you give your file upload fields the same name (for instance upload[]) Rack will merge them into a single parameter. The upload helper will keep this structure so that whenever the request parameter contains an array, the uploaded_files method will also return an array of Ramaze::Helper::Upload::UploadedFile objects for the same key.
@return [Hash] Currently uploaded files. The keys in the hash
corresponds to the names of the request parameters that contained file uploads and the values consist of Ramaze::Helper::Upload::UploadedFile objects.