views:

618

answers:

1

Given the html form ...

  <form id="mainform" action="/upload" enctype="multipart/form-data" method="post">
        <div>
            <input type="file" name="img"/>
        </div>
        <div>
            <input type="submit" value="Send">
        </div>
    </form>

... and the handler ...

class Picture(db.Model):
  image = db.BlobProperty()

class Submission(webapp.RequestHandler):
    def post(self):
        picture = Picture()
        image = self.request.get("img")
        picture.image = db.Blob(image)
        picture.put()
        self.redirect('/')

... is there any way within the handler to get the filename the user entered for the upload? In PHP, I can write refer to $_FILES['img']['name'], but I do not see what syntax, if any, would work with request.get. In another question, another author uses a javascript routine in his html page to extract the filename the user chooses every time an OnChange event occurs, and then pass it separately in a hidden field. Is that necessary? PHP seems to get the filename for free.

+4  A: 

I uncovered a solution in the documentation for the cgi module:

picture.local_filename = self.request.POST[u'img'].filename

The request's POST object is a collection of FieldStorage, and FieldStorage objects have a filename attribute.

Thomas L Holaday