Flask raises UnicodeEncodeError (latin-1) when send attachment with UTF-8 characters Flask raises UnicodeEncodeError (latin-1) when send attachment with UTF-8 characters flask flask

Flask raises UnicodeEncodeError (latin-1) when send attachment with UTF-8 characters


The problem is that when using as_attachement=True the filename is sent in the headers. Unfortunately it seems that flask does not yet support rfc5987 which specifies how to encode attachment file names in a different encoding other than latin1.

The easiest solution in this case would be to drop as_attachement=True, then it won't be sent with a Content-Disposition header, which avoids this problem.

If you really have to send the Content-Disposition header you could try the code posted in the related issue:

    response = make_response(send_file(out_file))    basename = os.path.basename(out_file)    response.headers["Content-Disposition"] = \        "attachment;" \        "filename*=UTF-8''{utf_filename}".format(            utf_filename=quote(basename.encode('utf-8'))        )    return response

This should be fixed in the next release (>0.12)