2012-07-24 23 views
0

我是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

請指導我...

+0

你爲什麼要這樣做?在文件系統中存儲數據絕對沒有什麼特別之處 - 實際上你可以通過編寫一個你可以用'StringIO'或blobstore完成的文件來做任何事情。 – 2012-07-27 05:43:57

回答

3

從 「What is Google App Engine?

Applications cannot write to the file system in any of the runtime environments. An application can read files, but only files uploaded with the application code. The app must use the App Engine datastore, memcache or other services for all data that persists between requests. The Python 2.7 environment allows bytecode to be read, written, and modified.

根據應用程序的需要,您需要返回使用blobstore或嘗試Google Cloud Storage API

+0

謝謝..你的回覆.. – Saravanan 2012-07-25 13:32:11

相關問題