utf8'编解码器无法解码位置15的字节0x89:无效的起始字节 [英] utf8' codec can't decode byte 0x89 in position 15: invalid start byte

查看:1113
本文介绍了utf8'编解码器无法解码位置15的字节0x89:无效的起始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的项目的一个视图,我正在尝试使用boto将图像从本地系统上传到s3。

This is a view for my project in which I am trying to upload images from my local system to s3 using boto.

class ImageList(generics.ListCreateAPIView):
        queryset = Image.objects.all()
        serializer_class = ImageSerializer

        def post(self , request , format = None):
            # import ipdb; ipdb.set_trace()
            serializer = ImageSerializer(data = request.data)
            if serializer.is_valid():
                serializer.save()
                print request.data
                return Response({'received data' : request.data})
            return Response(serializer.errors , status = status.HTTP_400_BAD_REQUEST)

class ImageDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Image.objects.all()
    serializer_class = ImageSerializer

它工作正常对于django管理员,但是当我尝试使用Django Rest Framework html表单上传它时会产生错误。我已经覆盖了通用类视图中的post方法。生成的错误是:

It works fine for django admin but generates error while I try to upload it using Django Rest Framework html form. I have overridden the post method in generic class based view. The error that gets generated is :

UnicodeDecodeError at /image/
'utf8' codec can't decode byte 0xff in position 15: invalid start byte
Request Method: POST
Request URL:    http://127.0.0.1:8000/image/
Django Version: 1.9
Exception Type: UnicodeDecodeError
Exception Value:    
'utf8' codec can't decode byte 0xff in position 15: invalid start byte
Exception Location: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py in encode, line 210
Python Executable:  /usr/bin/python
Python Version: 2.7.10
Unicode error hint

The string that could not be encoded/decoded was: "����\u

我已经尝试了几乎所有可用的堆栈溢出,但没有为我工作,我没有从某个地方复制代码,所以没有奇怪的字符。

I have tried almost everything available on stack overflow but nothing worked for me. I didn't copy the code from somewhere so there would be no strange characters.

参考这个是 models.py file:

For reference this is the models.py file:

class Image(models.Model):
    image_meta = models.ForeignKey('Image_Meta',on_delete=models.CASCADE,)
    image = models.ImageField(upload_to='images-data')
    # image = models.URLField(max_length = 500)
    order = models.IntegerField()
    version = models.CharField(max_length=10)

    def __unicode__(self):
            return (self.image)

有关详细信息,请参阅:

See this for more detail :

推荐答案

JSON字符串是unicode字符串,而不是二进制字符串。您的图像包含二进制数据,并且JSON序列化程序正在抱怨。

JSON strings are unicode strings, not binary strings. Your image contains binary data, and the JSON serializer is complaining about that.

解决此问题的一个常见方法是使用base64或base85。 Python通过 base64 模块内置了对这两种(和其他)编码的支持。

One common approach to work around this problem is to use base64 or base85. Python has builtin support for those two (and other) encodings via the base64 module.

这篇关于utf8'编解码器无法解码位置15的字节0x89:无效的起始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆