gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:14:08 25 4
gpt4 key购买 nike

这是我的代码,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)

最佳答案

您可能想为此使用 blobstore 服务;我写了一系列帖子( 123 ),介绍如何使用上传小部件将多个文件上传到 blobstore。

关于python - 如何在谷歌应用引擎上使用html5上传多个文件(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com