我想处理访客上传的文件,这是视图代码:
def charset(request):
logging.info('charset')
name = request.GET['name']
file_path = os.path.join(settings.MEDIA_ROOT, name)
logging.info(file_path)
logging.info(type(file_path))
file1 = open(file_path.decode('utf8'), 'wb')
file1.write(b'test')
file1.close()
return HttpResponse('success')
但是发生了错误。追溯如下:
UnicodeEncodeError at /upload/charset/
'ascii' codec can't encode characters in position 27-28: ordinal not in range(128)
您可以通过以下方式重复该错误:我的错误网站
我正在使用环境,因为:
python 3.4.3
django 1.9.3
apache2 2.4.7
但是当我使用运行服务器时python3 manage.py runserver 0.0.0.0:8000
。有用。
我怎样才能解决这个问题?感谢您的关注。您可以在github_of_my_django_tutorial网站上查看项目代码。
您需要先将文件名编码为UTF-8:
name = name.encode("utf8")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句