November 14th, 2013, 10:58 PM
Selecting files using python/cgi
I would like to browse and select a file via HTML, python and CGI. The HTML code is the following:
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="Upload" /></p>
And the python code is:
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
message = 'No file was uploaded'
""" % (message,)
Now I would like to browse only those files with the "csv" extension. Is there a way to do that, please?
Thanks for any help!