更新时间:2024-06-19 GMT+08:00
在AstroZero脚本中,如何返回zip压缩文件内容?
返回zip压缩文件内容脚本示例如下:
import * as zip from 'zip' import * as buffer from 'buffer' import * as text from 'text'; import * as context from 'context'; let bytes = text.bytes('I want to be binary'); let buff = buffer.from("some data"); let fileMap = { "/testdir/test.txt": "this is the data in test.txt", "/testdir/demo/bytes.txt": bytes, "/testdir/demo/buff.txt": buff.bytes(), } let stream = zip.compression(fileMap); let resp = context.getHttp().response; resp.setHeader("Content-Type", "application/zip"); let size = len(stream); resp.setHeader('Content-Length', <string><any>size); // 这里只是把zip二进制流返回给浏览器,浏览器需要依据Content-Type来解析 resp.setBody(stream);
父主题: 应用后端开发