在使用flask进行文件下载时,发现不能使用中文名字,
后来网上搜索的答案基本上都是python2的解决方法
报错为:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal not in range(128)
# -*- coding: utf-8 -*-
reload(sys)
sys.setdefaultencoding("utf8")
下面附上python3解决方案
原本的attachment_filename 是没有办法用中文名的
我们首先需要倒入一个包
from urllib.parse import unquote, quote
filename= quote(f_name + '.csv')
rv = send_file('/home/hesheng/deal_with_data/2019年6月渠道总数据/{}.csv'.format(file_name), as_attachment=True, attachment_filename=filename)
rv.headers['Content-Disposition'] += "; filename*=utf-8''{}".format(filename)
return rv
这样就完美解决了