Uploading files from a server

Robert Brewer fumanchu at amor.org
Tue Apr 27 02:30:21 EDT 2004


drs wrote:
> "Edward Diener" <eldiener at earthlink.net> wrote in message
> news:m1gjc.3688$g31.2919 at newsread2.news.atl.earthlink.net...
> > What is the easiest way in Python in a web server to upload 
> a client file,
> > once the file name on the client's machine has been entered, to a
> directory
> > on the server ?
> 
> I think this is what you want ... this is for mod_python with 
> the publisher
> handler. It will allow a user to upload a file and save it on 
> a server.  It
> provides no protection or checking, however.  other server 
> configurations
> will be slightly different.
> 
> first, use a form tag and input tag like
> 
> <form method=POST action="./upload_function/" 
> ENCTYPE="multipart/form-data">
> <input type="file" id="uploaded_file" name="uploaded_file">
> <input type="submit">
> </form>
> 
> The above will get the file to your server.  then use an 
> upload function
> like:
> 
> def upload_function(req, uploaded_file=None):
>     if uploaded_file:
>         fn1 = str(uploaded_file.filename).split('\\')[-1]
>         fn = fn1.split('/')[-1]
>         # the weird file splitting is because os.path on 
> Freebsd, where this
> ran,
>         # didn't deal with win32 and unix file paths for 
> uploaded files
>         d = uploaded_file.read()
>         f = open('/save/path/%s' % fn, 'w')
>         f.write(d)
>         f.close()
>         return 'uploaded'
>     else: return 'no file'

...and if you don't know beforehand if you have an uploaded file, try
something like:

from mod_python import apache, util
    
    def read(self, request):
        # Retrieve the submitted CGI parameters from Apache/mod_python.
        # ONLY CALL ONCE per request.
        newData = util.FieldStorage(request, 1).list
        self.requestParams.clear()
        if newData is not None:
            for eachParam in newData:
                name = self.unicode(eachParam.name)
                if eachParam.filename:
                    fileData = eachParam.file.read()
                    filename = self.unicode(eachParam.filename)
                    self.requestParams[name] = (filename, fileData)
                else:
                    value = self.unicode(eachParam.value)
                    self.requestParams[name] = value


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list