Class: ActionDispatch::Http::UploadedFile
Relationships & Source Files | |
Inherits: | Object |
Defined in: | actionpack/lib/action_dispatch/http/upload.rb |
Overview
Action Dispatch HTTP UploadedFile
Models uploaded files.
The actual file is accessible via the #tempfile accessor, though some of its interface is available directly for convenience.
Uploaded files are temporary files whose lifespan is one request. When the object is finalized Ruby unlinks the file, so there is no need to clean them with a separate maintenance task.
Class Method Summary
- .new(hash) ⇒ UploadedFile constructor Internal use only
Instance Attribute Summary
-
#content_type
rw
A string with the MIME type of the file.
-
#eof? ⇒ Boolean
readonly
Shortcut for
tempfile.eof?
. -
#headers
rw
A string with the headers of the multipart request.
-
#original_filename
rw
The basename of the file in the client.
-
#tempfile
rw
A
Tempfile
object with the actual uploaded file.
Instance Method Summary
-
#close(unlink_now = false)
Shortcut for
tempfile.close
. -
#open
Shortcut for
tempfile.open
. -
#path
Shortcut for
tempfile.path
. -
#read(length = nil, buffer = nil)
Shortcut for
tempfile.read
. -
#rewind
Shortcut for
tempfile.rewind
. -
#size
Shortcut for
tempfile.size
. - #to_io
-
#to_path
Shortcut for
tempfile.to_path
.
Constructor Details
.new(hash) ⇒ UploadedFile
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 31
def initialize(hash) # :nodoc: @tempfile = hash[:tempfile] raise(ArgumentError, ":tempfile is required") unless @tempfile @content_type = hash[:type] if hash[:filename] @original_filename = hash[:filename].dup begin @original_filename.encode!(Encoding::UTF_8) rescue EncodingError @original_filename.force_encoding(Encoding::UTF_8) end else @original_filename = nil end if hash[:head] @headers = hash[:head].dup begin @headers.encode!(Encoding::UTF_8) rescue EncodingError @headers.force_encoding(Encoding::UTF_8) end else @headers = nil end end
Instance Attribute Details
#content_type (rw)
A string with the MIME type of the file.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 22
attr_accessor :content_type
#eof? ⇒ Boolean
(readonly)
Shortcut for tempfile.eof?
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 98
def eof? @tempfile.eof? end
#headers (rw)
A string with the headers of the multipart request.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 29
attr_accessor :headers
#original_filename (rw)
The basename of the file in the client.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 19
attr_accessor :original_filename
#tempfile (rw)
A Tempfile
object with the actual uploaded file. Note that some of its interface is available directly.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 26
attr_accessor :tempfile
Instance Method Details
#close(unlink_now = false)
Shortcut for tempfile.close
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 73
def close(unlink_now = false) @tempfile.close(unlink_now) end
#open
Shortcut for tempfile.open
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 68
def open @tempfile.open end
#path
Shortcut for tempfile.path
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 78
def path @tempfile.path end
#read(length = nil, buffer = nil)
Shortcut for tempfile.read
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 63
def read(length = nil, buffer = nil) @tempfile.read(length, buffer) end
#rewind
Shortcut for tempfile.rewind
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 88
def rewind @tempfile.rewind end
#size
Shortcut for tempfile.size
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 93
def size @tempfile.size end
#to_io
[ GitHub ]# File 'actionpack/lib/action_dispatch/http/upload.rb', line 102
def to_io @tempfile.to_io end
#to_path
Shortcut for tempfile.to_path
.
# File 'actionpack/lib/action_dispatch/http/upload.rb', line 83
def to_path @tempfile.to_path end