4

I am trying to upload some picture to Google App Engine using the Blobstore. And some of the files contain non-ascii characters. When I download these files, the filename for these downloaded file appeared to show the "key" in blobstore, rather than the original file name.

My site is http://wlhunaglearn.appspot.com/

I already add a save_as=blob_info.filename in my BlobstoreDownloadHandler , but it failed when the file name contains non-ascii characters.

Any suggestions? Thanks in advance.

The following is my main.py file

# -*- encoding: utf-8 -*-
import os
import urllib
import webapp2

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers


class MainHandler(webapp2.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body>')
        self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
        self.response.out.write("""Upload File: <input type="file" multiple name="file"><br> <input type="submit"
        name="submit" value="Submit"> </form></body></html>""")


class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
        blob_info = upload_files[0]
        self.redirect('/serve/%s' % blob_info.key())


class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info, save_as=blob_info.filename)


app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/upload', UploadHandler),
                               ('/serve/([^/]+)?', ServeHandler)],
                              debug=True)
Aaron
  • 2,383
  • 3
  • 22
  • 53
  • 2
    I am not quite sure what kind of "string" is a `BlobInfo`'s `filename` (a Unicode one, I think -- you can easily check that with a simple `logging.info` of `type(blob_info.filename)`!) and what kind of "string" is required for the `save_as` parameter (an arbitrary string of **bytes**, I believe) -- rather than delay response while exhaustively researching that I'm posting to point out that exactly this is the whole of the issue. If my beliefs are right, `save_as=blob_info.filename.encode('utf8')` will solve all; else, some logging & reporting tracebacks will help me help you! – Alex Martelli Feb 07 '15 at 22:46
  • Thank you @AlexMartelli , I have followed your instruction and found that what you guess are right. But the downloaded file name still not the original file name if it contains non-ascii charactors. See my post updates. – Aaron Feb 08 '15 at 04:46

1 Answers1

4

Searching all the posts, I finally got a hint from an Answer to another Question

I figure out the correct way to display non-ascii file name is to

add a urllib.quote function to the last line of ServeHandler class


Therefore the ServeHandler class will be:

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info, save_as=urllib.quote(blob_info.filename.encode('utf-8')))
Community
  • 1
  • 1
Aaron
  • 2,383
  • 3
  • 22
  • 53