[mod_python] how do I uplaod a file - code snippets sought

dave at davebritton.com dave at davebritton.com
Sat Oct 14 17:43:31 EDT 2006


Thanks, just what I needed - I've gotten it working now.
-Dave
Quoting Alex Greif <alex.greif at gmail.com>:

> Hi I use the following snippet to upload my files. Additionally a
> foldername is given, but this is only my usecase:
>
> def uploadFile(req, myFile=None, folderName=None):
>    """
>    Uploads the file and then the file will be written in the given folder.
>    """
>    req.content_type = "text/html"
>    folderPath = _util.getUploadFolderPath( \
>                                          
> uploadRootPath=config.upload_root_path,
>                                        folder=folderName)
>    fileName = myFile.filename.replace('\\','/').split('/')[-1]
>    req.log_error("httpUpload.uploadFile  file: %s/%s" %   
> (folderName,fileName),
>                            apache.APLOG_INFO)
>    filePath = os.path.join(folderPath,  fileName)
>    fileHandle = open(filePath, 'wb')
>    size = 0
>    while True:
>        data = myFile.file.read(8192)
>        if not data:
>            break
>        fileHandle.write(data)
>        size += len(data)
>        #~ req.log_error("%s - write chunk" % myFile.filename,
> apache.APLOG_INFO)
>    fileHandle.close()
>    os.chmod(filePath, stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP |
> stat.S_IWGRP)
>
>    template = psp.PSP(req, filename='_httpupload/psp/uploadFinished.psp')
>    template.run({
>                'size':size,
>                'fileName':fileName,
>                'folderName':folderName,
>                'type':myFile.type})
>
>
> HTH Alex.
>
> On 10/14/06, Dave Britton <dave at davebritton.com> wrote:
>>
>>
>> What is the way to use mod_python.utils to get a file object create and
>> uploaded from an html form? I can't quite get my head around it and I can't
>> find any useful snippets of code to copy. I'd appreciate any help,
>> especially an example. Thanks!
>>
>> Say I have an html form like this:
>> """<form action="upload" method="POST" enctype="multipart/form-data">
>> File name: <input name="file" type="file"><br>
>> <input name="submit" type="submit" value = "Send this file to the web
>> server">
>> </form>"""
>>
>> What does upload.py have to do to process the file whose name is submitted?
>>
>> the only cookbook version I have found is for  regular python in cgi mode:
>>
>> ==================
>> form = cgi.FieldStorage()
>>    if not form.has_key(form_field):
>>        print HTML_TEMPLATE2% {'SCRIPT_NAME':os.environ['SCRIPT_NAME']}
>>        return
>>    fileitem = form[form_field]
>>    if not fileitem.file:
>>        return
>>    fname=os.path.basename(fileitem.filename)
>>    print "fileitem.filename=%s,<br> uploadddir=%s,<br> fout=%s" \
>>       %(fileitem.filename, upload_dir, os.path.join(upload_dir, fname) )
>>
>>    fout = file (os.path.join(upload_dir, fname), 'wb')
>>    while 1:
>>        chunk = fileitem.file.read(100000)
>>        if not chunk: break
>>        fout.write (chunk)
>>    fout.close()
>> ===============
>> How do I adapt this to use mod_python FieldStorage ? Especially for early
>> mod_python as the installation I need this for has version 2.7 on Apache
>> 1.3, debian linux
>>
>>
>> Dave Britton
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
>>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python





More information about the Mod_python mailing list