UnicodeDecodeError与Django的请求.FILES [英] UnicodeDecodeError with Django's request.FILES
本文介绍了UnicodeDecodeError与Django的请求.FILES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在视图调用中有以下代码。
I have the following code in the view call..
def view(request):
body = u""
for filename, f in request.FILES.items():
body = body + 'Filename: ' + filename + '\n' + f.read() + '\n'
在某些情况下,我得到
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 7470: ordinal not in range(128)
我做错了什么? (我正在使用Django 1.1。)
What am I doing wrong? (I am using Django 1.1.)
谢谢。
推荐答案
Django有一些实用程序处理这个(smart_unicode, force_unicode,smart_str)。通常你只需要smart_unicode。
Django has some utilities that handle this (smart_unicode, force_unicode, smart_str). Generally you just need smart_unicode.
from django.utils.encoding import smart_unicode
def view(request):
body = u""
for filename, f in request.FILES.items():
body = body + 'Filename: ' + filename + '\n' + smart_unicode(f.read()) + '\n'
这篇关于UnicodeDecodeError与Django的请求.FILES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文