This is my code :
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
#from login import htmlPrefix,get_current_user
class MyModel(db.Model):
blob = db.BlobProperty()
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.get('file')
obj = MyModel()
obj.blob = db.Blob(file.encode('utf8'))
obj.put()
self.response.out.write('upload ok')
class download(BaseRequestHandler):
def get(self):
#id=self.request.get('id')
o = MyModel.all().get()
#self.response.out.write(''.join('%s: %s <br/>' % (a, getattr(o, a)) for a in dir(o)))
self.response.out.write(o)
application = webapp.WSGIApplication(
[
('/?', upload),
('/download',download),
],
debug=True
)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
My index.html is :
<form action="/" method="post">
<input type="file" name="file" />
<input type="submit" />
</form>
And it show :
<__main__.MyModel object at 0x02506830>
But I don't want to see this, I want to download it.
How to change my code to run?
Thanks
updated
It is ok now :
class upload(BaseRequestHandler):
def get(self):
self.render_template('index.html',)
def post(self):
file=self.request.get('file')
obj = MyModel()
obj.blob = db.Blob(file)
obj.put()
self.response.out.wri开发者_开发知识库te('upload ok')
class download(BaseRequestHandler):
def get(self):
#id=self.request.get('id')
o = MyModel.all().order('-').get()
#self.response.out.write(''.join('%s: %s <br/>' % (a, getattr(o, a)) for a in dir(o)))
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(o.blob)
And new question is :
if you upload a 'png' file, it will show successful, but when I upload a rar file I will run error. So how to set Content-Type automatically and what is the Content-Type of the 'rar' file?
Thanks
See Nick's blog for a working example.
Basically on upload you need to pull the file from self.request.POST
instead of get
in order to get the more useful cgi.FieldStorage
object. You can then read the mime type from file.type
, save the type string to your entity along with the blob, and write it out with the response headers on download.
Another option is to save file.name
to your entity on upload, and then plug it into mimetypes.guess_type()
to guess a type based on the file extension upon download.
What kind of data is stored in your blob? Is it an image? Because you would need to convert it to an image before writing it back in the response.
What you're doing now is writing the object straight to the response, which results in it printing a string with the object's address and type.
[edit] The correct content type for RAR files would be application/x-rar-compressed
. And I don't think you can automatically determine the content type from a blob. You would have to manually set the correct content type for each file type you want users to be able to download.
精彩评论