CSDN博客地址

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)调用来将文件对象的读写位置“倒回”到开头,否则传输失败

posted @ 2020-04-23 16:26  Yi_warmth  阅读(549)  评论(0编辑  收藏  举报
CSDN博客地址