Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding problem in app engine when submitting multipart/form-data forms

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.

like image 577
Símon Óttar Vésteinsson Avatar asked Sep 02 '10 06:09

Símon Óttar Vésteinsson


People also ask

What encoding is multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What encoding type should an HTML form use to be able to upload files to a web server?

enctype='multipart/form-data is an encoding type that allows files to be sent through a POST. Quite simply, without this encoding the files cannot be sent through POST. If you want to allow a user to upload a file via a form, you must use this enctype.

What is the meaning of Enctype multipart form data in PHP?

Definition and Usage The enctype attribute specifies how the form-data should be encoded when submitting it to the server. Note: The enctype attribute can be used only if method="post" .


1 Answers

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
Í
like image 168
Wilfred Hughes Avatar answered Nov 01 '22 19:11

Wilfred Hughes