问题
I'm trying to Upload a text file using :
<input type="file" name="file">
and this file is retrieved using:
class UploadHandler(webapp2.RequestHandler):
def post(self):
file=self.request.POST['file']
self.response.headers['Content-Type'] = "text/plain"
self.response.write(file.value)
my output is:
Content-Type: text/plain MIME-Version: 1.0 Content-Length: 312 Content-MD5: MDIzYzM5YmNmOWRmMzY5Zjk2MTYzZTUzNjYwMTg5YjM= content-type: text/plain content-disposition: form-data; name="file"; filename="blah.txt" X-AppEngine-Upload-Creation: 2013-04-24 07:57:23.729774
Is there any way i could retrive the file content instead of above headers. ??
回答1:
The following seems to work, so there must be something else that is happening (live example):
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = "text/html"
self.response.write('''
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit">
</form>
''')
class UploadHandler(webapp2.RequestHandler):
def post(self):
file = self.request.POST['file']
self.response.headers['Content-Type'] = "text/plain"
self.response.write(file.value)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/upload', UploadHandler)
], debug=True)
来源:https://stackoverflow.com/questions/16186583/retrieving-txt-file-contents-in-google-appengine