UnicodeDecodeError with Django's request.FILES

后端 未结 4 1791
自闭症患者
自闭症患者 2021-02-09 04:28

I have the following code in the view call..

def view(request):
    body = u\"\"  
    for filename, f in request.FILES.items():
        body = body + \'Filename         


        
4条回答
  •  清歌不尽
    2021-02-09 05:08

    you are appending f.read() directly to unicode string, without decoding it, if the data you are reading from file is utf-8 encoded use utf-8, else use whatever encoding it is in.

    decode it first and then append to body e.g.

    data = f.read().decode("utf-8")
    body = body + 'Filename: ' + filename + '\n' + data + '\n'
    

提交回复
热议问题