使用Django进行下载zip出现乱码
1 将文件进行压缩成zip
```python
def zip_dir(folder_path, images_path, vis_path, fig_path, chinese_path, english_path):
name = str(time.time())
folder_name = os.path.join(folder_path, "{0}.zip".format(name))
file_list = []
for i in range(len(images_path)):
file_list.append(images_path[i])
file_list.append(vis_path[i])
file_list.append(fig_path[i])
file_list.append(chinese_path[i])
file_list.append(english_path[i])
zf = zipfile.ZipFile(folder_name, "w", zipfile.zlib.DEFLATED)
for tar in file_list:
arc_name = tar[len(folder_path):]
zf.write(tar, arc_name)
zf.close()
return folder_name, name
2 传输zip
def file_iterator(filename, chunk_size=512):
with open(filename, 'rb') as f:
while True:
c = f.read(chunk_size)
if c:
yield c
else:
break
def download_sub(request):
// 省略获得文件路径images_root, images_path, vis_path, fig_path, chinese_path, english_path等代码
folder_name, name = zip_dir(images_root, images_path, vis_path, fig_path, chinese_path, english_path)
response = StreamingHttpResponse(file_iterator(folder_name))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}.zip"'.format(name)
return response
3 网页响应