在这里,您尝试将字节(使用’rb’模式打开的文件)转换为字符串:
text = str(files.read())
将以上行改为:
text = files.read().decode(encoding='change_to_source_file_encoding')
然后你可以使用以下命令将unicode字符串转换为utf-8字节字符串:
text = text.encode('UTF-8')
如果源编码是utf-8,您只需将files.read()中的字节字符串传递给结果字符串,而无需无意义的解码/编码步骤(从utf-8字节到字符串再到utf-8字节)…
url = 'url'
files = {'file': open("WebContent/"+fileName, 'rb')}
response = requests.post(url, files=files)
更新2:
根据响应头中的内容类型(… Content-Type:image / gif …),您可以在files.read()之后获得所需格式的数据,而无需任何编码/解码!