我是google appengine的新手。 我想上傳一個文件到一個目錄而不是存儲爲blob。想使用python上傳文件到google appengine中的目錄
main.py
import cgi
import webapp2
from google.appengine.api import users
from google.appengine.ext.webapp.template \
import render
from os import path
class MainHandler(webapp2.RequestHandler):
def get(self):
context={}
tmpl = path.join(path.dirname(__file__), 'static/html/index.html')
self.response.out.write(render(tmpl, context))
def post(self):
form_data = self.request.get('file')
file_data = form_data
f=open('static/html/'+form_data,'w')
f.write(file_data)
f.close
routes=[
(r'/', MainHandler),
]
app = webapp2.WSGIApplication(routes=routes,debug=True)
的index.html
> <html>
> <head><title>test</title></head>
> <body>hello
> <form id="addmovieform" action="/" method="post" ENCTYPE="multipart/form-data">
> <input type="file" name="file" >
> <input type="submit" name="submit">
> </form>
> </body>
> </html>
錯誤
Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in call rv = self.handle_exception(request, response, e) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in call rv = self.router.dispatch(request, response) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher return route.handler_adapter(request, response) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in call return handler.dispatch() File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch return self.handle_exception(e, self.app.debug) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch return method(*args, **kwargs) File "/Users/saravase/test/main.py", line 33, in post f=open('static/html/'+form_data,'w') File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 589, in init raise IOError('invalid mode: %s' % mode) IOError: invalid mode: w
請指導我...
你爲什麼要這樣做?在文件系統中存儲數據絕對沒有什麼特別之處 - 實際上你可以通過編寫一個你可以用'StringIO'或blobstore完成的文件來做任何事情。 – 2012-07-27 05:43:57