Python CGI Programming : File Upload Example:
To upload a file, the HTML form must have the enctype attribute set to multipart/form-data. The input tag with the file type creates a "Browse" button.
<html>
<body>
<form enctype="multipart/form-data"
action="save_file.py" method="post">
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
</body>
</html>
Above example has been disabled intentionally to save
people uploading file on our server, but you can try
above code with your server.
Here is the script save_file.py to handle file upload −
form = cgi.FieldStorage()
Get filename here.
fileitem = form['filename']
Test if the file was uploaded
if fileitem.filename:
strip leading path from file name to avoid
directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
Sample Projects : https://github.com/SadaLearningHub1/P...
In questa pagina del sito puoi guardare il video online Lesson - 69 : Python Advanced - Python CGI Programming : File Upload Example della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Sada Learning Hub 28 gennaio 2018, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 1,420 volte e gli è piaciuto 12 spettatori. Buona visione!