python文件上傳

1.前端代碼html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8888/login" method="post" enctype="multipart/form-data">
            <input type="file" name="file" /><br />
            <input type="submit" value="Upload" />
        </form>
</body>

</html>

2.後臺接收文件的代碼(flask)前端

from flask import Flask, jsonify, request, json
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route("/login", methods=["POST"])
def epwk_generate():
    username = request.values.get("username")
    file_hello = request.files
    f = request.files["file"]
    # 上傳文件
    filename = secure_filename(f.filename)
    f.save('./hh.' + str(filename))
    return 'ok'

if __name__ == '__main__':
    app.run(host='127.0.0.1',port=8888, debug=False)
相關文章
相關標籤/搜索