13
votes

I have a simple form that submits a image to the blobstore and a title for the image. This works on my local devserver but when I deploy my code, non ascii letters in the title become garbled with some kind of mixture of ascii and hex. For example Ísland becomes =CDsland. Note, I am using <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> as the first value In the header. Also utf-8 works for all my other forms. Just the multipart form that becomes garbled. Anyways this is my form:

<form action="{{ uploadurl }}" enctype="multipart/form-data" method="post">
  <div><label>Title</label><input type="text" name="title" class="string" /></div>
  <div><label>Picture</label><input type="file" name="img"/></div>
  <div style="margin-top:10px;"><input type="submit" value="Add picture" /></div>
  <input type="hidden" value="{{ album.key }}" name="alid"/>
</form>

And this is the class handling the form:

# handler for posting photos
class PostPhoto(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files =  self.get_uploads('img')
        photourl = images.get_serving_url(str(upload_files[0].key()))
        photo = Photo()
        #because of multipart/form-data
        photo.title = self.request.get("title")
        photo.photourl = photourl
        photo.photoalbum = PhotoAlbum.get(self.request.get('alid'))     
        photo.put()

Does anyone have a clue how I can fix this? Do I have to do some server side encoding/decoding? I have tried googling around for that with no results(python newb), so this is my last resort before i just alter my design and split up the forms.

6
What are you using to determine what the title is on the server? And does this work if you post directly to a handler, without using blobstore? - Nick Johnson
That the meta content type tag is present in top of the HTML head is completely irrevant whenever your webpage is served over HTTP. A bit decent webbrowser will use the one in the HTTP response header instead. See also w3 spec w3.org/TR/html4/charset.html#h-5.2.2 - BalusC
As for now appegine SDK 1.7.2 it is not fixed yet but the producrion behavior changed slightly: 'Ísland' becomes 'zXNsYW5k'. unicode(base64.b64decode('zXNsYW5k'), 'iso_8859-2') - keiw

6 Answers

5
votes

This is a known bug. http://code.google.com/p/googleappengine/issues/detail?id=3761

Getting back to the original data is a matter of:

>>> import quopri
>>> t = unicode(quopri.decodestring('=CD'), 'iso_8859-2')
>>> print t
Í
5
votes

I'm using Django nonrel and fixed it with this middleware:

http://code.google.com/p/googleappengine/issues/detail?id=2749#c33

import logging
import quopri
log = logging.getLogger(__name__)

class BlobRedirectFixMiddleware(object):
    def process_request(self, request):
        if request.method == 'POST' and 'HTTP_X_APPENGINE_BLOBUPLOAD' in request.META and request.META['HTTP_X_APPENGINE_BLOBUPLOAD'] == 'true':
            request.POST = request.POST.copy()
            log.info('POST before decoding: %s' % request.POST)
            for key in request.POST:
                if key.startswith('_') or key == u'csrfmiddlewaretoken':
                    continue
                value = request.POST[key]
                if isinstance(value,(str, unicode)):
                    request.POST[key] = unicode(quopri.decodestring(value), 'iso_8859-2')
            log.info('POST after decoding: %s' % request.POST) 
        return None
2
votes

=CD is the quoted-printable representation of Í.

I have no explanation as to why the production server would be giving you this data as quoted-printable when the dev_appserver doesn't, but the quopri module from the standard library can decode it for you.

2
votes

This comment explains how to fix the issue and includes the appengine_config.py, which makes everything work: http://code.google.com/p/googleappengine/issues/detail?id=2749#c21

I don't include the code here because I have no idea how to attach file, and it's quite big to be included inline.

1
votes

have you tried photourl = images.get_serving_url(unicode

(upload_files[0].key())) insted of photourl = images.get_serving_url(str(upload_files[0].key()))

1
votes

Add the newer webob library to your app.yaml :

libraries:
- name: webapp2
  version: "2.5.2"
- name: webob
  version: "1.2.3"

Got it from: https://code.google.com/p/googleappengine/issues/detail?id=2749