如何在谷歌应用引擎(python)上使用html5上传多个文件

时间:2010-11-24 17:17:53

标签: python google-app-engine html5 upload

这是我的代码,upload.py:

class MyModel(db.Model):
    data = db.BlobProperty(required=True)
    mimetype = db.StringProperty(required=True)

class BaseRequestHandler(webapp.RequestHandler):
  def render_template(self, filename, template_args=None):
    if not template_args:
      template_args = {}
    path = os.path.join(os.path.dirname(__file__), 'templates', filename)
    self.response.out.write(template.render(path, template_args))

class upload(BaseRequestHandler):
  def get(self):
    self.render_template('index.html',)
  def post(self):
    file = self.request.POST['file']
    obj = MyModel(data=file.value, mimetype=file.type)
    obj.put()
    o=file
    #self.response.out.write(''.join('%s: %s <br/>' % (a, getattr(o, a)) for a in dir(o)))
    #return
    file_url = "http://%s/download/%d/%s" % (self.request.host, obj.key().id(), file.filename)
    self.response.out.write("Your uploaded file is now available at <a href='%s'>%s</a>" % (file_url,file_url,))

并且index.html是:

<form enctype="multipart/form-data" action="/" method="post">
  <input type="file" name="file" multiple="true" />
  <input type="submit" />
</form>

您可以使用file = self.request.POST['file']获取一个文件,但使用html5多文件

如何在发布时使用python获取多文件?

感谢

上传

现在没关系,请按照这篇文章:Receive multi file post with google app engine

class Download_file(db.Model):
    data = db.BlobProperty(required=True)
    mimetype = db.StringProperty(required=True)
    download_url = db.StringProperty()

class BaseRequestHandler(webapp.RequestHandler):
  def render_template(self, filename, template_args=None):
    if not template_args:
      template_args = {}
    path = os.path.join(os.path.dirname(__file__), 'templates', filename)
    self.response.out.write(template.render(path, template_args))

class upload(BaseRequestHandler):
    def get(self):
        files=Download_file.all()
        self.render_template('index.html',{'files':files})
    def post(self):
        files = self.request.POST.multi.__dict__['_items']
        #self.response.out.write(files)
        for file in files:
            file=file[1]
            obj = Download_file(data=file.value, mimetype=file.type)
            obj.put()
            file_url = "http://%s/download/%d/%s" % (self.request.host, obj.key().id(), file.filename)
            file_url = "<a href='%s'>%s</a>" % (file_url,file_url,)
            obj.download_url=file_url
            obj.put()
            self.response.out.write("Your uploaded file is now available at %s </br>" % (file_url))

class download(BaseRequestHandler):
    def get(self,id,filename):
        #id=self.request.get('id')
        entity = Download_file.get_by_id(int(id))
        self.response.headers['Content-Type'] = entity.mimetype
        self.response.out.write(entity.data)

2 个答案:

答案 0 :(得分:1)

AFAIK,request.POST['file']应该是文件字典,即POST['file']应该有密钥作为上传文件的名称,值应该分别是文件的内容,即{{1} }。

我不知道GAE HTTP Request类提供的功能是什么,但它应该与此一致。无论如何,POST['file']['avatar.png'] = ... # raw image data中的肯定是,somwhere!

编辑:

好的,我刚刚注意到GAE为你创建了一个文件对象,我的猜测是应该的:

self.request

答案 1 :(得分:1)

您可能希望为此使用blobstore服务;我写了一系列帖子(123),内容涉及如何使用上传小部件将多文件上传到blobstore。