One - One Code All

Blog Content

python Flask,上传文件,request.files

Python   2018-06-23 21:51:43

python Flask,上传文件,request.files


demo.py(上传文件)

# coding:utf-8
 
from flask import Flask, request  # 导入request对象
 
app = Flask(__name__)
 
@app.route("/upload", methods=["POST"])
def upload():
    file_obj = request.files.get("pic")  # "pic"对应前端表单name属性
    if file_obj is None:
        # 表示没有发送文件
        return "未上传文件"
 
    # 将文件保存到本地
    # # 1. 创建一个文件
    # f = open("./demo.png", "wb")
    # # 2. 向文件写内容
    # data = file_obj.read()
    # f.write(data)
    # # 3. 关闭文件
    # f.close()
 
    # 直接使用上传的文件对象保存
    file_obj.save("./demo1.png")
    return "上传成功"
 
 
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True)



上一篇:ValueError: 时间数据与格式 '%Y/%m/%d %H:%M:%S' 不匹配
下一篇:mac中pandas安装不成功,报错its parent directory is not owned by the current user

The minute you think of giving up, think of the reason why you held on so long.