[英]'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte django
so i have variable contains with bytes and i want to write it to text and download it.所以我有包含字节的变量,我想将它写入文本并下载它。 but to use write, it's must in bytes.
但要使用写入,它必须以字节为单位。 so how to make from bytes to string?
那么如何从字节到字符串呢?
now i got error like this.现在我得到了这样的错误。 i tried to decode it, but it cannot works.
我试图解码它,但它无法工作。
'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte
“utf-8”编解码器无法解码 position 中的字节 0x8b 0:起始字节无效
here's the code:这是代码:
def create_file(f):
print(f) #f = b'\x8b\x86pJ'
response = HttpResponse(content_type="text/plain")
response['Content-Disposition'] = 'attachment; filename=file.txt'
filename = f
print(filename) # filename = b'\x8b\x86pJ'
download = filename.decode('utf-8')
response.write(download)
print(response)
return response
You can try你可以试试
def create_file(f):
print(f) #f = b'\x8b\x86pJ'
response = HttpResponse(content_type="text/plain")
response['Content-Disposition'] = 'attachment; filename=file.txt'
filename = f
print(filename) # filename = b'\x8b\x86pJ'
download = filename.decode('latin-1')
response.write(download)
print(response)
return response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.