Aykhan Amiraslanli Aykhan Amiraslanli - 1 year ago 177
Python Question

python CGI variable json

I need to show uploaded file size in JSON format on Python.
Here is my code:

def save_uploaded_file (form_field, upload_dir):

form = cgi.FieldStorage()
if not form.has_key(form_field): print 0; return
fileitem = form[form_field]
if not fileitem.file: print 0; return
varfilepath = os.path.join(upload_dir, fileitem.filename)
fout = file (varfilepath, 'wb')
while 1:
chunk = fileitem.file.read(100000)
if not chunk: break
fout.write (chunk)
varfilesize = os.path.getsize(varfilepath)
data['filesize'] = varfilesize
print simplejson.dumps(data)

save_uploaded_file ("file_1", UPLOAD_DIR)

But I got error:

<type 'exceptions.NameError'>: global name 'data' is not defined
args = ("global name 'data' is not defined",)
message = "global name 'data' is not defined"

Answer Source

You will have to create the map data:

data = {'filesize': varfilesize}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download