Meteor 流星+;铁制路由器&x2B;JSZip不工作

Meteor 流星+;铁制路由器&x2B;JSZip不工作,meteor,iron-router,jszip,Meteor,Iron Router,Jszip,我正在尝试使用Meteor创建和提供一个zip文件。以下是我所拥有的: Router.map -> @route "data", where: 'server' path: "/data" action: -> this.response.writeHead 200, 'Content-Type': 'application/zip' 'Content-Disposition': "attachment; fil

我正在尝试使用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'
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'

使用默认的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'
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'