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...
Auf dieser Seite können Sie das Online-Video Lesson - 69 : Python Advanced - Python CGI Programming : File Upload Example mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Sada Learning Hub 28 Januar 2018 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 1,420 Mal angesehen und es wurde von 12 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!