【问题标题】:Send a JSON and a zip attachment in the same response with cherrypy使用cherrypy 在同一响应中发送 JSON 和 zip 附件
【发布时间】:2015-08-14 22:18:57
【问题描述】:

我想在同一个 HTTP 响应(使用cherrypy)中发送一个 JSON 响应和一个 zip 附件。有可能吗?

现在我的代码是:

def GET(self, **params):
    result, data = getData(params)
    cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="resp.zip"'
    return data

我可以用它下载附件。如何也发送 JSON 消息?

【问题讨论】:

  • 所以你想在相同的响应中发送jsonzip两种内容类型,对吧?
  • 没错,JSON作为响应和zip在同一类型..有可能吗?
  • 我不这么认为,因为它是 2 个不同的 Content-Type 标头值。我会像@andrew-kloos 提到的那样做。返回一个json,其中包含指向zip 文件的链接。

标签: python json rest attachment cherrypy


【解决方案1】:

好的,你需要做这样的事情......

$.ajax({
    url: 'your-url-that-gives-zip-file.html',
    dataType: 'JSON',
    success: function(response){
        if(response.zip) {
            location.href = "GetZip?=zip" + response.zip;
        }
    }
});

你的樱桃处理程序:

@cherrypy.expose 
def GetZip(self, zip=""):
    return """
    <!-- Get your zip file code with and combine with http://youradress.com -->
    <a href='http://youraddress.com/downloads/' + zip
    """

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 2015-10-04
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多