flask 文件上传实例

后端:

from flask import Flask, request, render_template
import os

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        headimg = request.files['headimg']
        print headimg
        print(os.path.join(r'/home/walker/demo',headimg.filename))
        headimg.save(os.path.join(r'/home/walker/demo',headimg.filename))
        return 'filename is %s ' % headimg.filename
    else:
        return render_template('upload.html')

前端:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <form action="" method="post" enctype="multipart/form-data">
       upload: <input type="file" name="headimg">
        <input type="submit" name="upload">
    </form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/MissLong/article/details/82150993