0

我是谷歌应用引擎的新手。我想将文件上传到目录而不是存储为 blob。

主文件

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)

索引.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(最近一次通话最后):文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py”,第 1536 行,调用 rv = self .handle_exception(request, response, e) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py”,第 1530 行,通话中 rv = self.router.dispatch(request, response) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py”,第 1278 行,在default_dispatcher return route.handler_adapter(request, response) 文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py”,第 1102 行,通话中 return handler.dispatch() File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py",第 572 行,在 dispatch return self.handle_exception( e、self.app.debug)文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py”,第570行,在调度返回方法中( *args, **kwargs) 文件“/Users/saravase/test/main.py”,第 33 行,在帖子 f=open('static/html/'+form_data,'w') 文件“/Applications/GoogleAppEngineLauncher. app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py”,第 589 行,在初始化 raise IOError('invalid mode: %s' % mode) IOError: invalid mode: w

请指导我...

4

1 回答 1

3

来自“什么是 Google App Engine?

应用程序无法在任何运行时环境中写入文件系统。应用程序可以读取文件,但只能读取使用应用程序代码上传的文件。应用程序必须使用 App Engine 数据存储区、内存缓存或其他服务来处理请求之间持续存在的所有数据。Python 2.7 环境允许读取、写入和修改字节码。

您需要重新使用 blobstore,或尝试使用Google Cloud Storage API,具体取决于您的应用程序的需要。

于 2012-07-24T18:29:40.437 回答