Re: sample file upload
Brandon Long <[email protected]> Fri, 8 Aug 2008 15:32:44 -0700
| Newsgroups | gmane.text.clearsilver.general |
|---|---|
| Organization | Fiction L Networks |
| Message-ID | <20080808223244.GB19458@bl1> |
Do you have a perferred language?
Attached is a test python one I had lying around. upload.py is a dirt
simple one, upload2.py uses a callback so you get information as the
uplaod is going on.
Brandon
On 08/08/08 maxsimon1980 uttered the following other thing:
> hello every one,
>
> can anyone send me a sample code for file up load.
> please send me the code for cgi and html part.
>
> Regards
> Max
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
--
"A fundamental law: no matter how good you are, someone somewhere
believes that you're going to Hell." -- Andrew C. Bulhak
http://www.fiction.net/blong/
upload2.py
(text/plain, 1.3 KB)
#!/usr/local/bin/python2.2
import neo_cgi
import sys, os
import traceback
def exceptionString():
import StringIO
## get the traceback message
sfp = StringIO.StringIO()
traceback.print_exc(file=sfp)
exception = sfp.getvalue()
sfp.close()
return exception
class Page:
def __init__(self):
self.ncgi = neo_cgi.CGI()
self.ncgi.setUploadCB(None, self.upload_cb)
try:
os.unlink('/tmp/cstest.txt')
except os.error:
pass
try:
os.unlink('/tmp/file')
except os.error:
pass
self.ncgi.parse()
def upload_cb(self, foo, nread, length):
f = open('/tmp/cstest.txt', 'a')
f.write("Uploaded %d out of %d\n" % (nread, length))
f.close()
return 0
def display(self, file):
if self.ncgi.hdf.getValue("Query.file", ""):
try:
fp = self.ncgi.filehandle("file")
data = fp.read()
f = open("/tmp/file", "w")
f.write(data)
except neo_cgi.CGIFinished:
return
except Exception, Reason:
sys.stderr.write("Python Exception: %s\n" % (str(repr(Reason))))
s = neo_cgi.text2html("Python Exception: %s" % exceptionString())
self.ncgi.error(s)
self.ncgi.display(file)
sys.stdout.write('<pre>%s</pre>' %
neo_cgi.htmlEscape(self.ncgi.hdf.dump()))
p = Page()
p.display("hello.cst")
hello.cst
(text/plain, 200 B)
<html> <title>Test upload</title> <body> <form name=upload method=POST enctype="multipart/form-data"> <input type=file name=file> <input type=submit name=Action.Upload value="Upload"> </body> </html>
upload.py
(text/plain, 835 B)
#!/usr/bin/env python
import sys, os, traceback, string
import neo_cgi
def log (s):
sys.stderr.write("CGI: %s\n" % s)
def exceptionString():
import StringIO
## get the traceback message
sfp = StringIO.StringIO()
traceback.print_exc(file=sfp)
exception = sfp.getvalue()
sfp.close()
return exception
def main (argv, environ):
# log ("starting")
cgi = neo_cgi.CGI("")
try:
fp = cgi.filehandle("file")
print "Content-Type: text/plain\r\n\r\n"
data = fp.read()
print data
f = open("/tmp/file", "w")
f.write(data)
except neo_cgi.CGIFinished:
return
except Exception, Reason:
log ("Python Exception: %s" % (str(repr(Reason))))
s = neo_cgi.text2html("Python Exception: %s" % exceptionString())
cgi.error (s)
if __name__ == "__main__":
main (sys.argv, os.environ)