问题描述:

I'm uploading an xml file using multipart/form/data and need to read the xml file content and print the xml file content in the response.write()

MAIN_PAGE_HTML = """\

<html>

<body>

<form action="/f1" method="post" enctype="multipart/form-data">

<div><input type="file" name="filecoll"></div>

<div><input type="submit" value="Submit "></div>

</form>

</body>

</html>

"""

class MainPage(webapp2.RequestHandler):

def get(self):

self.response.write(MAIN_PAGE_HTML)

class formfiless(webapp2.RequestHandler):

def post(self):

self.response.write('<html><body>You wrote:<pre>')

self.response.write(cgi.escape(self.request.get('filecoll')))

f=self.request.get('filecoll')

data=f.read()

self.response.write('</pre></body></html>')

application = webapp2.WSGIApplication([

('/',MainPage),

('/f1',formfiless),

], debug=True)

when I tried to read the form file the following error occured.

Traceback (most recent call last):

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__

rv = self.handle_exception(request, response, e)

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__

rv = self.router.dispatch(request, response)

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher

return route.handler_adapter(request, response)

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__

return handler.dispatch()

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch

return self.handle_exception(e, self.app.debug)

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch

return method(*args, **kwargs)

File "C:\Users\uniphoreC08\Desktop\upload_grammar\helloworld.py", line 35, in post

f.read()

AttributeError: 'unicode' object has no attribute 'read'

网友答案:

The call

self.request.get('filecoll')

appears to be yielding a unicode string, not a file object. In the next line, you are calling the read() method as if it were a file object. Hence

AttributeError: 'unicode' object has no attribute 'read'

相关阅读:
Top