class TestView(APIView): def get(self,request): try: response = HttpResponse(content_type='application/zip') zf = zipfile.ZipFile(response,'w') zf.writestr("qqqq","nihao") response['Content-Disposition'] = 'attachment; filename=myfile.zip' return response except Exception as e: return Response(str(e))