0

나는 다중/폼/데이터를 사용하고 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) 
)합니다 (Response.Write에있는 XML 파일의 내용을 인쇄 할 필요가 xml 파일을 업로드하고 있습니다

다음 오류가 발생하여 양식 파일을 읽으려고했습니다.

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' 
+0

당신이 할 수 있습니다 질문은 명확하게, 정확히 무엇을하려고하는지 묻습니다. 어쩌면 좀 더 정교해질 수도 있습니다. – Deepend

+0

왜이 질문 가스에 태그를 지정 했습니까? –

+0

또한 webapp2가 아닌 webapp2 일 수도 있습니다. 묽고 싱거운. 아이들은 요즘 ... –

답변

0

호출

self.request.get('filecoll') 

는 유니 코드 문자열이 아닌 파일 객체를 산출 할 것으로 보인다. 다음 줄에서는 파일 객체 인 것처럼 read() 메서드를 호출합니다. 따라서

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

0

다음은 XML 파일을 웹에 문자열로 쓰는 샘플 코드입니다. 몸 바이트 문자열로 본문 내용과 body_file 동일한 데이터에 파일 같은 인터페이스 제공 : 대신 요청 가지 인자의

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)) 

는 요청 몸과 body_file를 사용