0

I am trying to implement a file upload solution using app engine and python. The thing which I am struggling with checking whether there is actually a file attached to the form or not. I am setting the enctype="multipart/form-data" and in principle it works. My python handler looks like this:

fileupload = self.request.POST["content"]

if not fileupload:
    return self.error(400)

This works if there is no file attached. However if there is a file attached it gives the following error:

  File "D:\eclipse_dev\workspace\test\src\handlers.py", line 351, in post
    if not fileupload:
  File "C:\Python25\lib\cgi.py", line 633, in __len__
    return len(self.keys())
  File "C:\Python25\lib\cgi.py", line 609, in keys
    raise TypeError, "not indexable"
TypeError: not indexable

How can I safely check that an upload is present before doing anything else in the handler?

Thanks for any help.

Regards, Robin

4 Answers 4

1

How about:

fileupload = self.request.POST["content"]

if fileupload is None:
    return self.error(400)
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this, but it does not trigger the error. I'm quite new to python. Is there any otherway of checking the value of a variable.
0

Well, I've got this working using:

import cgi
.
.
fileupload = self.request.POST["content"]

if not isinstance(fileupload, cgi.FieldStorage):
   return self.error(400)

Not sure if that's the best solution, but it seems to work.

1 Comment

This works for me so I am going to accept my own answer. I hope that's OK.
0

App Engine's self.request.get() method handles POST data just fine:

"The request object provides a get() method that returns values for arguments parsed from the query and from POST data."

Try this instead:

def post(self):
  fileupload = self.request.get('content')
  if not fileupload:
    self.error(400)
  else:
    # Proceed normally

Comments

0

I have a small tutorial about file upload to GAE. http://verysimplescripts.blogspot.jp/ There is a completely working solution included as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.