42.5.1. Posílání souboru

<% require 'cgi'; @cgi = CGI.new  %>

<% p @cgi.params['drop'].first %>
<form method='post' enctype='multipart/form-data'>
Drop Location: <input type=file name=drop>
<input type=submit name=submit>
</form>

Uploading was not as hard as I had thot! Attached is a file that should get you going uploading and included in text below....good luck !

Soubor upload.rbx

=begin

        upload.rbx is meant to be illustrative and might not
serve your purposes but should
        get you started if you need to upload with
Ruby/modruby

        {{{http://www.rubycentral.com/book/lib_network.html


        Multipart Form Values

        When dealing with a multipart form,
        the array returned by CGI#[] is composed of objects
of class Tempfile,
        with the following dynamically added methods:

                original_filename
                local_path
                content_type
                size

        To make this file work you'll need to...

                make it executable
                make your upload directory writable(Upload::PATH)

                ...and that _should_ be it

        }}}

=end


begin

        def main
        #{{{
                require 'cgi'

                $cgi = CGI.new("html4")

                $cgi.header('content-type'=>'text/html')

                puts '<html><body>'

                if $cgi.params.length == 0

                        Upload.form
                else

                        Upload.post

                        print Upload.getStatus

                end

                puts '</body></html>'

        end#}}}



rescue Exception=>e

        puts e

end

class Upload
#{{{ -----------------------Uploads file to server
from html form--------------------------

        #max size of file
        MAX_SIZE     = 25000

        #where the file goes / MUST HAVE WRITE PRIVS HERE
        PATH             = "/YOUR_FILE_PATH_HERE_PLEASE/"

        #how many file inputs - you can upload multiple files
at once
        FILE_COUNT       = 5

        #what file types do we allow?
        CONTENT_TYPES= ['image/jpg','image/gif']

        #how are things going?
        @@status         = []

        def self.form
        #{{{

                puts '<form method="post"
enctype="multipart/form-data">'

                FILE_COUNT.times do

                        puts '<br/><input type="file" name="myfile">'

                end

                 puts '<p/><input type="submit"></form>'

        end#}}}

        def self.post
        #{{{

                $cgi['myfile'].each do |incoming|

                        if incoming.size == 0

                                @@status << "<br/>Avoiding empty field"
                                next

                        end

                        if incoming.size > MAX_SIZE

                                @@status << "<br/>Data too large for
#{incoming.original_filename}(#{incoming.size} >
#{MAX_SIZE})"
                                next

                        end

                        #need to strip :)...trailing space...ouch
                        if not CONTENT_TYPES.include?
incoming.content_type.strip

                                @@status << "<br/>Type not allowed(type =
#{incoming.content_type}) allowed content =
#{CONTENT_TYPES.join(' | ')}"
                                next

                        end

                        # all should be ok to upload
                        path     = PATH + incoming.original_filename

                        File.new(path.untaint,'w') do |file|

                                file << incoming.read

                        end

                        @@status << "<br/>done writing #{path}"
                end

        end#}}}

        def self.getStatus
        #{{{

                @@status

        end#}}}
end#}}}

main

I've had to change the last upload.rbx file...hey i said i was a rookie...i've had to make these changes...

#File.new(path.untaint,'w') do |file|

        #file << incoming.read

#end

#to

file = File.new(path.untaint,'w')
file << incoming.read
file.close

apparently using the second is not allowing the write << from the read any idea why? The first worked but only created a null file. What am i missing? Was it not closing? Seems to work now but no promises ;)

Licence Creative Commons
Tento dokument Ruby, jehož autorem je Radek Hnilica, podléhá licenci Creative Commons Uveďte autora-Nevyužívejte dílo komerčně-Zachovejte licenci 3.0 Česká republika .