美文网首页
flask 文件下载api

flask 文件下载api

作者: 高峥 | 来源:发表于2019-12-22 19:59 被阅读0次
    from flask import Flask, send_from_directory, make_response
    import tablib
    
    app = Flask(__name__)
    
    
    @app.route('/upload')
    def upload():
        # data = 'a,c,v,s,s \r' 'e,r,et,t,e' 转csv
        js = [{"a": "111", "b": "222"}, {"a": "333", "b": "444"}]
        js_2 = [{"a": "aaa", "b": "bbb"}, {"a": "nnn", "b": "mmm"}]
    
        header = tuple(js[0].keys())
    
        data = []
        # 循环里面的字典,将value作为数据写入进去
        for row in js:
            body = []
            for v in row.values():
                body.append(v)
            data.append(tuple(body))
            
        data_2 = []
        for row in js_2:
            body = []
            for v in row.values():
                body.append(v)
            data_2.append(tuple(body))
    
        data = tablib.Dataset(*data, headers=header, title='aaa')
        data_2 = tablib.Dataset(*data_2, headers=header, title='bbb')
        excel = tablib.Databook([data, data_2]).export('xls')
    
        # filename = "核武库" + startDate.strftime("%Y%m%d") + "-" + endDate.strftime("%Y%m%d") + ".xls"
        response = make_response(excel)
        response.headers["Content-Disposition"] = "attachment; filename=myfilename.xls" # 浏览器作为附件下载
        response.headers['Content-Type'] = 'xls'
        return response
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    
    

    相关文章

      网友评论

          本文标题:flask 文件下载api

          本文链接:https://www.haomeiwen.com/subject/wozonctx.html