1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # FROM: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/273844 |
---|
4 | |
---|
5 | import cgi |
---|
6 | import cgitb; cgitb.enable() |
---|
7 | import os, sys |
---|
8 | import string |
---|
9 | |
---|
10 | UPLOAD_DIR = "/tmp/upload/" |
---|
11 | form = cgi.FieldStorage() |
---|
12 | |
---|
13 | dbg = [] |
---|
14 | |
---|
15 | def debug(dbgstr): |
---|
16 | dbg.append(str(dbgstr)) |
---|
17 | |
---|
18 | def save_uploaded_file(form_field, upload_dir): |
---|
19 | global form |
---|
20 | if not form.has_key(form_field): |
---|
21 | debug("didn't find it! (1)") |
---|
22 | return |
---|
23 | fileitem = form[form_field] |
---|
24 | if not fileitem.file: |
---|
25 | debug(form.getvalue(form_field, "")) |
---|
26 | debug(fileitem.__dict__) |
---|
27 | debug("didn't find it! (2)") |
---|
28 | return |
---|
29 | fout = file(os.path.join(upload_dir, fileitem.filename), 'wb') |
---|
30 | while 1: |
---|
31 | chunk = fileitem.file.read(100000) |
---|
32 | if not chunk: break |
---|
33 | fout.write (chunk) |
---|
34 | fout.close() |
---|
35 | |
---|
36 | retval = "false"; |
---|
37 | fileFields = "" |
---|
38 | |
---|
39 | if form.has_key("fileFields"): |
---|
40 | fval = str(form.getvalue("fileFields", "")) |
---|
41 | fileFields = fval.split(",") |
---|
42 | debug("'fileCount': '" + str(len(fileFields)) + "',") |
---|
43 | for field in fileFields: |
---|
44 | debug("'fileField' : '"+field + "',") |
---|
45 | # Uncomment the line below to really test file save. |
---|
46 | # You may need to modify UPLOAD_DIR above. |
---|
47 | # save_uploaded_file(str(field).strip(), UPLOAD_DIR) |
---|
48 | retval = "true"; |
---|
49 | |
---|
50 | debug("'retval': " + retval) |
---|
51 | |
---|
52 | print """Content-Type: text/html |
---|
53 | |
---|
54 | |
---|
55 | <html> |
---|
56 | <head> |
---|
57 | </head> |
---|
58 | <body> |
---|
59 | <textarea style="width: 100%%; height: 100px;">{ %s }</textarea> |
---|
60 | </body> |
---|
61 | </html> |
---|
62 | """ % (string.join(dbg, "\n")) |
---|