其中index.html
文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image and Text Upload</title>
</head>
<body>
<h1>Upload Image and Text</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="text">Text:</label>
<input type="text" id="text" name="text" required><br><br>
<label for="image">Image:</label>
<input type="file" id="image" name="image" accept="image/*" required><br><br>
<button type="submit">Upload</button>
</form>
</body>
</html>
和app.py
文件:
from flask import Flask, request, render_template
import os
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
text = request.form['text']
image = request.files['image']
if image:
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
image.save(image_path)
return f'Text: {text}, Image saved at: {image_path}'
if __name__ == '__main__':
app.run(debug=True)
运行app.py
后,在http://127.0.0.1:5000/
访问