Popular recipes by Wilhelm Shen http://code.activestate.com/recipes/users/4061228/2010-02-24T13:50:27-08:00ActiveState Code RecipesSingleUpload.py (Python)
2010-02-24T13:50:27-08:00Wilhelm Shenhttp://code.activestate.com/recipes/users/4061228/http://code.activestate.com/recipes/521897-singleuploadpy/
<p style="color: grey">
Python
recipe 521897
by <a href="/recipes/users/4061228/">Wilhelm Shen</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>Usage:</p>
<pre class="prettyprint"><code>#!/usr/bin/env python2.5
#
# html file:
# <form action="test.cgi" method="POST" enctype="multipart/form-data">
# <input name="file1" type="file"><input type="submit"></form>
# test.cgi: ...
import SingleUpload
def upload():
fr = SingleUpload.open()
fw = open('/tmp/%s' %fr.filename, 'wb')
while True:
l = fr.read(65536)
if not l:
break
fw.write(l)
fw.flush()
fw.close()
print 'Content-Type: text/plain\r\n\r\n'
try:
upload()
print 'OK'
except IOError:
__import__('traceback').print_exc(
file=__import__('sys').stdout )
</code></pre>