from flask import Flask,render_template,request
import os
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('upload.html')
@app.route('/upload',methods=['POST'])
def upload():
file = request.files['file']
if os.path.exists(os.path.join(os.path.join(os.path.dirname(__file__),'uploads'),file.filename)):
return '文件已经存在'
else:
file.save('uploads/%s' % file.filename)
return '文件已经保存'
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form method="post" action="{{ url_for('upload') }}" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
</body>
</html>