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...
En esta página del sitio puede ver el video en línea Lesson - 69 : Python Advanced - Python CGI Programming : File Upload Example de Duración hora minuto segunda en buena calidad , que subió el usuario Sada Learning Hub 28 enero 2018, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 1,420 veces y le gustó 12 a los espectadores. Disfruta viendo!