|
|
@@ -0,0 +1,50 @@
|
|
|
+注:同次提交下的esp32程序配套的上传拍照文件的python flask程序,提供上传接口。
|
|
|
+
|
|
|
+
|
|
|
+from flask import Flask, request, jsonify
|
|
|
+import os
|
|
|
+import datetime # 导入 datetime 模块
|
|
|
+
|
|
|
+app = Flask(__name__)
|
|
|
+
|
|
|
+# 配置上传文件夹
|
|
|
+UPLOAD_FOLDER = 'uploads'
|
|
|
+app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
+
|
|
|
+# 设置允许上传的文件类型
|
|
|
+ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
+
|
|
|
+# 检查文件扩展名是否允许上传
|
|
|
+def allowed_file(filename):
|
|
|
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
+
|
|
|
+# 上传照片的路由
|
|
|
+@app.route('/upload', methods=['POST'])
|
|
|
+def upload_file():
|
|
|
+ if 'file' not in request.files:
|
|
|
+ return jsonify({'message': 'No file part'}), 400
|
|
|
+
|
|
|
+ file = request.files['file']
|
|
|
+
|
|
|
+ # 如果用户没有选择文件
|
|
|
+ if file.filename == '':
|
|
|
+ return jsonify({'message': 'No selected file'}), 400
|
|
|
+
|
|
|
+ if file and allowed_file(file.filename):
|
|
|
+ timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
|
|
|
+ extension = file.filename.rsplit('.', 1)[1].lower()
|
|
|
+ filename = f"{timestamp}.{extension}"
|
|
|
+ # 生成保存路径
|
|
|
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
|
+ file.save(file_path)
|
|
|
+ return jsonify({'message': 'File uploaded successfully', 'filename': filename}), 200
|
|
|
+
|
|
|
+ return jsonify({'message': 'File not allowed'}), 400
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ # 创建上传文件夹
|
|
|
+ if not os.path.exists(UPLOAD_FOLDER):
|
|
|
+ os.makedirs(UPLOAD_FOLDER)
|
|
|
+
|
|
|
+ # 启动 Flask 应用,并指定端口为 7002
|
|
|
+ app.run(debug=True, host='0.0.0.0', port=7002)
|