def tarball(request, release): file_name = 'dj-download-%s.tar.gz' % release file_path = os.path.join(FILE_FOLDER, file_name) try: tarball_file = open(file_path) except IOError: raise Http404 wrapper = FileWrapper(tarball_file) response = HttpResponse(wrapper, content_type='application/zip') response['Content-Encoding'] = 'utf-8' # 设置该值gzip中间件就会直接返回而不进行后续操作 response['Content-Disposition'] = 'attachment; filename=%s' % file_name return response #//python/9053