views:

28

answers:

2

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

A: 

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.

tomlog
hi tomlog, look the updated.
zjm1126
+1  A: 

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.

Drew Sears