0

我正在尝试使用 Meteor 创建和提供一个 zip 文件。这是我所拥有的:

Router.map ->
  @route "data",
    where: 'server'
    path: "/data"
    action: ->
      this.response.writeHead 200,
        'Content-Type': 'application/zip'
        'Content-Disposition': "attachment; filename=data.zip"
      zip = new JSZip()
      zip.file "Hello.txt", "Hello World"
      this.response.end zip.generate({ 'type': 'string', 'compression': 'DEFLATE'})

我有 jszip.min.js 和 jszip-deflate.js。创建了一个 zip 文件,我可以下载它,但我无法使用存档管理器打开该文件(已损坏)。如果我用文本编辑器打开 data.zip,我会看到“PK”加上两个十六进制字符。

如何创建一个 zip 文件并返回它?

回答:

使用默认的 base64 编码进行压缩,并在 end/write 方法中指定响应编码:

Router.map ->
  @route "data",
    where: 'server'
    path: "/data"
    action: ->
      this.response.writeHead 200,
        'Content-Type': 'application/zip'
        'Content-Disposition': "attachment; filename=data.zip"

      zip = new JSZip()
      zip.file "Hello.txt", "Hello World"
      file = zip.generate({ 'compression': 'DEFLATE' })

      this.response.end file, 'base64'
4

1 回答 1

1

使用默认的 base64 编码进行压缩,并在 end/write 方法中指定响应编码:

Router.map ->
  @route "data",
    where: 'server'
    path: "/data"
    action: ->
      this.response.writeHead 200,
        'Content-Type': 'application/zip'
        'Content-Disposition': "attachment; filename=data.zip"

      zip = new JSZip()
      zip.file "Hello.txt", "Hello World"
      file = zip.generate({ 'compression': 'DEFLATE' })

      this.response.end file, 'base64'
于 2014-02-13T22:20:20.480 回答