how to upload muti file using html5 on google app engine(python)
this is my code, the 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,))
and the index.html is :
<form enctype="multipart/form-data" action="/" method="post">
<input type="file" name="file" multiple="true" />
<input type="submit" />
</form>
you can use file = self.request.POST['file']
to get 开发者_如何转开发one file , but using html5 muti file ,
how to get muti file using python when post ?
thanks
upload
it is ok now , Follow this article: 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)
You probably want to use the blobstore service for this; I wrote a series of posts (1, 2, 3) covering how to do multiple-file upload to the blobstore, using an upload widget.
AFAIK, the request.POST['file']
should be a dictionary of files, i.e. POST['file']
should have keys as names of the files uploaded and the value should be the contents of the files respectively, i.e. POST['file']['avatar.png'] = ... # raw image data
.
I don't know what's the functionality provided by the GAE HTTP Request class, but it should be consistent to this. Whatever the case, it's definitely in self.request
, somwhere!
EDIT:
Ok, I just noticed GAE creates a file object for you, my guessing is this should work:
for file in POST['file']:
obj = MyModel(data=file.value, mimetype=file.type)
obj.put()
精彩评论