zoukankan      html  css  js  c++  java
  • Flask-将链接url内容压缩成zip文件返回给前端

    首先定义下载函数

    import asyncio
    def download_url(url):
        new_loop = asyncio.new_event_loop()
        asyncio.set_event_loop(new_loop)
        loop = asyncio.get_event_loop()
        task = asyncio.ensure_future(asyc_step(url))
        loop.run_until_complete(asyncio.wait([task]))
        zip_data = task.result()
        return zip_data
    
    @app.route("/download", methods=["POST"])
    def download():
        body = request.json
        if 'url' not in body or 'zip_file' not in body:
            return response_fail(msg="查询参数不全")
        else:
            if body['url'] and content_length(body['url']):
                data = download(body['url'])
                data.seek(0)
                send_file(data, attachment_filename=body['zip_file'], as_attachment=True)
                return response_success(msg="传送成功")
            else:
                return response_fail(msg="Json_Body的url不是一个有效的文件链接")
    

    注意点:
    需要data.seek(0)调用来将文件对象的读写位置“倒回”到开头,否则传输失败

  • 相关阅读:
    Visual Studio 2015 密钥
    Vue-next源码新鲜出炉一
    vue2.0基础整理
    Nest
    Nest
    Nest
    Nest
    Nest快速上手
    element-plus源码分析第一节
    获取视频第一帧,作为封面图
  • 原文地址:https://www.cnblogs.com/zhouzetian/p/13380547.html
Copyright © 2011-2022 走看看