简体   繁体   English

在webapp2中读取表单文件-google appengine

[英]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.

相关问题 Appengine Python Webapp2 API逻辑结构 - Appengine Python Webapp2 API Logical Structure 在appengine上使用webapp2创建用户配置文件 - Creating a user profile using webapp2 on appengine webapp2-只读文件系统错误 - webapp2 - read-only file system error Google AppEngine webapp2:导入bigquery时,没有名为pkg_resources的模块错误 - Google AppEngine webapp2: No module named pkg_resources error when importing bigquery 如何为Google AppEngine Webapp2 Urls进行SEO网址更改和重定向? - How to do SEO Url changes and Redirects for google AppEngine Webapp2 Urls? Visual Code pylint:无法导入 webapp2 和 google.appengine.api - Visual Code pylint: unable to import webapp2 and google.appengine.api 使用Google Appengine Python(Webapp2),我需要使用OpenID Connect对Microsoft的新V2端点进行身份验证 - Using Google Appengine Python (Webapp2) I need to authenticate to Microsoft's new V2 endpoint using OpenID Connect Webapp2列出(和/或杀死)用户的所有会话(Appengine Python) - Webapp2 List (and/or kill) All Sessions for User (Appengine Python) 在 appengine 之外运行 webapp2,如何将其作为服务/守护程序运行? - Running webapp2 outside of appengine, how to run it as a service/deamon? 继承Webapp2 Google App引擎错误 - Inheriting webapp2 google App engine error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM