def download(self, request, pk=None):
import qrcode
instance = self.get_object()
response = HttpResponse(content_type='application/zip') # 指明下载的文件格式
response['Content-Disposition'] = 'attachment;filename=QrCode.zip'
with zipfile.ZipFile(response, "w", zipfile.ZIP_DEFLATED) as zip_fp:
for _ in QrCode.objects.filter(business_apply=instance):
img = qrcode.make(SERVER_URL+str(_.uuid))
with zip_fp.open(f"{_.uuid}.png", "w") as f:
img.save(f)
return response
------------------------------------------
业务需求后期变化 需要支持大数据下载
解决方法后端使用异步生成上传七牛并生成任务表 给前端提前返回文件下载连接
前端 轮询七牛 (反正不是我的服务器)当文件不是404 就下载