zoukankan      html  css  js  c++  java
  • Flask 处理文件 file

    介绍

    我们在开发过程中,总会与文件进行打交道。上传文件 or 下载文件。解析文件 or 生成文件。下面我们来了解一些我们经常使用的文件操作。

    首先,我们后端收到的文件都是由前端通过 post form-data 来上传文件的。

    文件的下载

    # 获取文件
    files = request.files.get("file")
    # 获取文件的名字
    files_name = files.filename
    # 保存文件在本地
    files.save(路径,名字)
    files.save(os.path.join(TEMPATE_PATC.encode("utf-8"), files_name.encode("utf-8")))

    注:我们在保存文件的时候也可以校验一下存储的路径和保存的文件类型

    传输文件到前端

    from flask import send_file
    # 存储路径加文件名字
    file_path = TEMPATE_PATC + unicode("ip.xlsx").encode("utf-8")
    return send_file(file_path, as_attachment=True)

    判断存储的路径

    if not os.path.exists(FILE_DIR):
        os.makedirs(FILE_DIR)

    修改文件的名字

    import datetime, uuid, os
    fileinfo = os.path.splitext("ip.txt")
    datetime.datetime.now().strftime("%Y%m%d%H%M%S")+str(uuid.uuid4().hex)+fileinfo[-1]
  • 相关阅读:
    合一算法最新版
    string.at(i)
    字符串逆转
    String
    Vector
    1005POJ
    但愿天堂一切都好
    合一算法
    合一算法2
    BTREE与其它索引的优缺点对比
  • 原文地址:https://www.cnblogs.com/shangwei/p/14415359.html
Copyright © 2011-2022 走看看