【FLASK】上传文件

## 后端:


import
os from flask import request, jsonify from werkzeug.utils import secure_filename from flask import Flask, request import hashlib import time app = Flask(__name__) @app.route('/upload', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': file = request.files['file'] print(file.filename) basepath = os.path.dirname(__file__) # 当前文件所在路径 # 加密文件名 file_name = md5(file.filename) + ".jpg" upload_path = os.path.join(basepath, 'static/', secure_filename(file_name)) # 储存路径 print(upload_path) file.save(upload_path) # 保存文件 dict_data = {"message": "上传成功"} return jsonify(dict_data) def md5(string): ctime = str(time.time()) m = hashlib.md5(bytes(string, encoding="utf-8")) m.update(bytes(ctime, encoding="utf-8")) return m.hexdigest() if __name__ == '__main__': app.run(debug=True)
# 前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>文件上传示例</h1>
    <form action="" enctype='multipart/form-data' method='POST'>
        <input type="file" name="file">
        <input type="submit" value="上传">
    </form>
</body>
</html>

 

posted @ 2020-09-23 16:58  PythonNew_Mr.Wang  Views(192)  Comments(0Edit  收藏  举报