[英]read form file in webapp2 -google appengine
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() 我正在使用multipart / form / data上传xml文件,需要读取xml文件内容并在response.write()中打印xml文件内容。
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. 似乎正在产生unicode字符串,而不是文件对象。 In the next line, you are calling the read() method as if it were a file object.
在下一行中,您将调用read()方法,就好像它是一个文件对象一样。 Hence
因此
AttributeError: 'unicode' object has no attribute 'read'
AttributeError:“ unicode”对象没有属性“ read”
Here is sample code which will write xml file as string in the web. 这是将在网络中将xml文件作为字符串写入的示例代码。
class MPost(webapp2.RequestHandler):
def post(self):
uploaded_file = self.request.body
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(str(uploaded_file))
instead of the request arguements, use the request body and and body_file: body is the body content as a byte string and body_file provides a file-like interface to the same data: 代替请求争论,使用请求正文和and body_file:body是正文内容,作为字节字符串,而body_file为相同数据提供类似于文件的接口:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.