【问题标题】:Meteor + Iron Router + JSZip not workingMeteor + Iron Router + JSZip 不工作
【发布时间】:2014-07-01 08:06:20
【问题描述】:

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

【问题讨论】:

    标签: meteor iron-router jszip


    【解决方案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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2013-11-21
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2015-02-27
      • 2017-06-17
      相关资源
      最近更新 更多