frasco de aprendizaje-subir imágenes a una carpeta especificada

Registre el proceso de implementación y el código

from flask import Flask,render_template,request,redirect,url_for
from werkzeug.utils import secure_filename
import os

app = Flask(__name__)

@app.route('/uppload', methods=['POST', 'GET'])
def upload():
	if request.method == 'POST':
		f = request.files['file']
		basepath = os.path.dirname(__file__)
		upload_path = os.path.join(basepath, 'static/images',secure_filename(f.filename))
		f.save(upload_path)
		return redirect(url_for('upload'))#重定向到页面
	return render_template('upload.html')


if __name__ == '__main__':
	app.run(debug=True)

código html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask上传图片演示</title>
</head>
<body>
<div>
    <h1>please upload a picture</h1>
   <form action="" enctype='multipart/form-data'method='POST'>
        <input type="file" name="file" id="imageUpload" accept=".png, .jpg, .jpeg"><br>
        <input type="submit" value="shangchuan">
    </form>
</div>
</body>
</html>

 

Publicado 17 artículos originales · me gusta 0 · vistas 3232

Supongo que te gusta

Origin blog.csdn.net/qq_31874075/article/details/88719983
Recomendado
Clasificación