First, I don't need any functions more than upload a file. No progress bar, no file type, or size check, no multiple files.
What I want is the most simple HTML webpage to handle the upload and save the file with the name I specified.
I tried to use this:
<form action="../cgi-bin/upload.py" method="post" enctype="multipart/form-data">
<input type="file" name="upload" />
<input type="submit" /></form>
In upload.py
:
#!/usr/bin/python
import os
import commands
import cgi, cgitb
cgitb.enable()
print "Content-Type: text/html"
print
print 'start!'
form = cgi.FieldStorage()
filedata = form['upload']
But I don't know how to save this in file, like "Beautiful.mp3".
Can any body help?
Though, really, I don't want to use any scripts. I just want the most basic html pages. Python scripts will only exist when there must be some CGI handlers. Flash is not preferred.
The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!
Use a label tag and point its for attribute to the id of the default HTML file upload button. By doing this, clicking the label element in the browser toggles the default HTML file upload button (as though we clicked it directly).
The filedata
object will wrap a file-like object that can be treated like a regular file. Basically you would do this:
if filedata.file: # field really is an upload
with file("Beautiful.mp3", 'w') as outfile:
outfile.write(filedata.file.read())
Or, you could do just about anything else with it, using read()
, readlines()
or readline()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With