import os
from flaskimport Flask, request, redirect, url_for,render_template
from werkzeug.utilsimport secure_filename
UPLOAD_FOLDER= '/home/liji/works/LJfile/test1'
ALLOWED_EXTENSIONS= set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app= Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/succe')
def succes():
return render_template('succe.html')
def allowed_file(filename):
return '.' in filenameand \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method== 'POST':
file= request.files['file']
if fileand allowed_file(file.filename):
filename= secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect('succe')
return render_template('upload.html')
if __name__=='__main__':
app.debug=True
app.run()
前端编写一个from表单即可
<!doctype html>
<title>Upload new File</title>
<h1>上传文件</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=上传>
</form>
网友评论