问题:如何批量下载文件并打包成压缩包进行下载?
解决:使用python内置模块zipfile
方法:
from zipfile import ZipFile
class DownloadHanlder(BaseHandler):
zipfile_path = os.path.realpath(
os.path.join(os.path.dirname(__file__),
"../../static/download/Data.zip"))
newzip = ZipFile(zipfile_path, "w")
user_uv = {'code':0, 'name': '用户数据.xls', 'file_path': '/static/download/user.xls'}
if not user_uv['code']:
newzip.write(self.absolute_path(user_uv['file_path']), user_uv['name'])
self.write({
'code': 0,
'file_path': '/static/download/Data.zip',
})
def absolute_path(self, relative_path):
abs_path = os.path.realpath(
os.path.join(os.path.dirname(__file__),
"../../%s" % relative_path))
return abs_path