R: [mod_python] problem in file download

Johnny Morano johnny.morano at vlaanderen.be
Thu Jun 6 10:37:45 EST 2002

On Thursday 06 June 2002 09:59, you wrote:
> 	fileDaSalvare=open(salva_su,'w')
> 	fileDaSalvare.write(form['file_costi'].value)
> 	fileDaSalvare.close()
> but work only with text-file

indeed .... change: fileDaSalvare=open(salva_su,'w') into 
fileDaSalvare=open(salva_su, 'wb')

'wb' stands for 'write binary' and this is required for non-textfiles in 

take a look at the following script (i called it fileUpload.py):
# fileUpload.py
from mod_python import util
from mod_python import apache

def form():
    output = """
    <form action="insert" method="post" enctype="multipart/form-data" 
        File: <input type="file" name="fileField"><br>
        <input type="submit">
    return output

def insert(req):
    import string
    import os

    form = req.form
    uploadDir = '/tmp/'

        fileData = form['fileField'].file.read()
        filePathName = uploadDir + form['fileField'].filename
        open(filePathName, 'wb').write(fileData)
        return '<html><body>file uploaded to '+filePathName+'</body></html>'
        return '<html><body>no file to upload</body></html>'

this works... or should work.. well, it works on my pc ;-)

hope this helps

Johnny Morano - Linux Consultant
 Ministerie Vlaamse Gemeenschap                    |
 Koning Albert II-laan, 7 (610) 1210 Brussel       |
 tel: 02/5531319 mail: johnny.morano at vlaanderen.be |

More information about the Mod_python mailing list