• Flask 上传文件


    后端

    @app.route('/index/', methods=['GET', 'POST'])
    def index():
        import os
        if request.method == 'GET':
            return render_template('index.html')
        # POST
        file_obj = request.files.get('code')
        # print(file_obj)     # <FileStorage: 'bbs.zip' ('application/zip')>
        # print(file_obj.filename)    # bbs.zip
        print(type(file_obj))   # <class 'werkzeug.datastructures.FileStorage'>
        # from werkzeug.datastructures import FileStorage   # 查看源码
        file_path = os.path.join(os.getcwd(), 'files', file_obj.filename)
        # print(file_path)
        file_obj.save(file_path)
        # save(self, dst, buffer_size=16384)  dst是目标文件,包括文件名
        return '上传成功'

    前端

    <form action="" method="post" enctype="multipart/form-data">
        <p><input type="file" name="code" id=""></p>
        <p><input type="submit" name="" value="上传"></p>
    
    </form>
  • 相关阅读:
    Redis单机操作
    Scala
    RDD算子
    Python学习之旅(七)
    python学习之旅(六)
    python学习之旅(五)
    python学习之旅(四)
    python学习之旅(二)
    python学习之旅(三)
    Python学习之旅(一)
  • 原文地址:https://www.cnblogs.com/wt7018/p/11608875.html
Copyright © 2020-2023  润新知