Python 逐块编码二进制文件失败
我需要编码的Zip文件似乎太重,下面的方法给了我错误:Python 逐块编码二进制文件失败,python,encode,binaryfiles,Python,Encode,Binaryfiles,我需要编码的Zip文件似乎太重,下面的方法给了我错误: with open("/tmp/pdf/pdffiles.zip", "rb") as f: binary_file = f.read() encoded = base64.b64encode(binary_file) self.download_zip = encoded 所以我试着将其分块,但最终下载的文件已损坏, 请大家看一下下面的代码并给我一些提示: zipfile = o
with open("/tmp/pdf/pdffiles.zip", "rb") as f:
binary_file = f.read()
encoded = base64.b64encode(binary_file)
self.download_zip = encoded
所以我试着将其分块,但最终下载的文件已损坏,
请大家看一下下面的代码并给我一些提示:
zipfile = open("/tmp/pdf/pdffiles.zip", "rb")
encoded = False
while True:
chunk = zipfile.read(8192)
if not chunk:
break
if encoded:
encoded += base64.b64encode(chunk)
else:
encoded = base64.b64encode(chunk)
zipfile.close()
self.download_zip = encoded
在对base64进行分块时,分块大小必须是6的倍数,否则数据将无法正确连接。您可以尝试像
8208这样的数字,它应该可以工作。对base64进行分块时,分块大小必须是6的倍数,否则数据将无法正确连接。您可以尝试一个像8208
这样的数字,它应该可以工作