Flask - handling unicode text with werkzeug? Flask - handling unicode text with werkzeug? flask flask

Flask - handling unicode text with werkzeug?


You should explictly pass unicode strings (type unicode) when dealing with non-ASCII data. Generally in Flask, bytestrings are assumed to have an ascii encoding.


I had a similar problem. I originally had this to send the file as attachment:

return send_file(dl_fd,                 mimetype='application/pdf',                 as_attachment=True,                 attachment_filename=filename)

where dl_fd is a file descriptor for my file.

The unicode filename didn't work because the HTTP header doesn't support it. Instead, based on information from this Flask issue and these test cases for RFC 2231, I rewrote the above to encode the filename:

response = make_response(send_file(dl_fd,                                   mimetype='application/pdf'                                   ))response.headers["Content-Disposition"] = \    "attachment; " \    "filename*=UTF-8''{quoted_filename}".format(        quoted_filename=urllib.quote(filename.encode('utf8'))    )return response

Based on the test cases, the above doesn't work with IE8 but works with the other browsers listed. (I personally tested Firefox, Safari and Chrome on Mac)


You should use something like:

@route('/attachment/<int:attachment_id>/<filename>', methods=['GET'])def attachment(attachment_id, filename):    attachment_meta = AttachmentMeta(attachment_id, filename)    if not attachment_meta:        flask.abort(404)    return flask.send_from_directory(        directory=flask.current_app.config['UPLOAD_FOLDER'],        filename=attachment_meta.filepath,    )

This way url_for('attachment',1,u'Москва 北京 תֵּל־אָבִיב.pdf') would generate:

/attachment/1/%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0%20%E5%8C%97%E4%BA%AC%20%D7%AA%D6%B5%D6%BC%D7%9C%D6%BE%D7%90%D6%B8%D7%91%D6%B4%D7%99%D7%91.pdf

Browsers would display or save this file with correct unicode name. Don't use as_attachment=True, as this would not work.