逐块编码二进制文件失败
Encoding binary file chunk by chunk fails
我需要编码的 Zip 文件似乎太重了,下面的方法给我错误:
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
这样的数字,它应该可以工作。
我需要编码的 Zip 文件似乎太重了,下面的方法给我错误:
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
这样的数字,它应该可以工作。