python - Uploading multiple files in Tornado -


i new tornado , trying make simple multiple file upload form, users able upload either 1 or more files.

here relevant part in upload.py

class uploadhandler(tornado.web.requesthandler):     def get(self):         files_dict = {}         self.render("upload_form.html", files_dict = files_dict)      def post(self):          ofn="" #original file name         ufn="" #uploaded file name         files_dict = {} # dict of original:uploaded names          #file1           if self.request.files['file1'][0]:                  file1 = self.request.files['file1'][0] #infput file                 ofn = file1['filename']                  extension = os.path.splitext(ofn)[1]                  if extension in ext:                     fname = ''.join(random.choice(string.ascii_lowercase + string.digits) x in range(8))                     ufn= fname+extension                      output_file = open("uploads/" + ufn, 'w')                     output_file.write(file1['body'])                     files_dict[ofn]= ufn                 else:                     files_dict[ofn]= none          #file2               if self.request.files['file2'][0]:              file2 = self.request.files['file2'][0] #infput file             ofn = file2['filename']              extension = os.path.splitext(ofn)[1]              if extension in ext:                 fname = ''.join(random.choice(string.ascii_lowercase + string.digits) x in range(8))                 ufn= fname+extension                  output_file = open("uploads/" + ufn, 'w')                 output_file.write(file1['body'])                 files_dict[ofn]= ufn             else:                 files_dict[ofn]= none                    self.render("upload_form.html", files_dict= files_dict ) 

and form in upload_form.html:

 <form enctype="multipart/form-data" action="/upload" method="post" class="center" > file1 <input type="file" name="file1" /> <br /> file2 <input type="file" name="file2" /> <br /> <input type="submit" value="upload" class="button" />  </form> 

this works fine when both file1 , file2 posted when either of them empty, errors this:

traceback (most recent call last):   file "/usr/local/lib/python2.7/dist-packages/tornado-3.1.1-py2.7.egg/tornado/web.py", line 1141, in _when_complete     callback()   file "/usr/local/lib/python2.7/dist-packages/tornado-3.1.1-py2.7.egg/tornado/web.py", line 1162, in _execute_method     self._when_complete(method(*self.path_args, **self.path_kwargs),   file "upload.py", line 41, in post     if self.request.files['file2'][0]: keyerror: 'file2' 

i tried different trick putting each file upload code in try,except block or add additional conditional check whether file's body exist in vain. appreciate hints.

you have keykerror because there not such key key in dictionary. try following code:

test_dict = {"a":"a"}  if test_dict['a']:     print "test_dict[a] - ok"  if test_dict['b']:     print "test_dict[b] - ok" 

output:

test_dict[a] - ok traceback (most recent call last):   file "test.py", line 6, in <module>     if test_dict['b']: keyerror: 'b' 

use in test whether have given key in dictionary:

def post(self):     ofn="" #original file name     ufn="" #uploaded file name     files_dict = {} # dict of original:uploaded names      #file1       if 'file1' in self.request.files:          file1 = self.request.files['file1'][0] #infput file         # ...      #file2         if 'file2' in self.request.files:          file2 = self.request.files['file2'][0] #infput file         # ...          # ... 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -