Want to upload a file to directory in google appengine using python

I am new to google appengine. I want to upload a file to a directory instead of storing as blob.

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>

Error

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/Goog leAppEngine-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

please guide me ...


From "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.

You'll need to return to using the blobstore, or try the Google Cloud Storage API, depending on the needs of your application.

链接地址: http://www.djcxy.com/p/88694.html

上一篇: App Engine开发环境无法启动应用程序

下一篇: 想要使用python将文件上传到google appengine中的目录