how to set Content-Type automatically when i download the data that i uploaded.

Posted by zjm1126 on Stack Overflow See other posts from Stack Overflow or by zjm1126
Published on 2010-06-17T07:32:14Z Indexed on 2010/06/17 9:53 UTC
Read the original article Hit count: 227

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.write('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

© Stack Overflow or respective owner

Related posts about python

Related posts about google-app-engine