46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
|
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
|
||
|
import os
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
app.config['UPLOAD_FOLDER'] = 'uploads'
|
||
|
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 限制上传文件大小为16MB
|
||
|
|
||
|
# 创建上传目录(如果不存在)
|
||
|
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
||
|
os.makedirs(app.config['UPLOAD_FOLDER'])
|
||
|
|
||
|
@app.route('/')
|
||
|
def index():
|
||
|
"""显示文件列表和上传表单"""
|
||
|
files = os.listdir(app.config['UPLOAD_FOLDER'])
|
||
|
return render_template('index.html', files=files)
|
||
|
|
||
|
@app.route('/upload', methods=['POST'])
|
||
|
def upload_file():
|
||
|
"""处理文件上传"""
|
||
|
if 'file' not in request.files:
|
||
|
return redirect(url_for('index'))
|
||
|
|
||
|
file = request.files['file']
|
||
|
if file.filename == '':
|
||
|
return redirect(url_for('index'))
|
||
|
|
||
|
# 保存文件
|
||
|
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
|
||
|
return redirect(url_for('index'))
|
||
|
|
||
|
@app.route('/download/<filename>')
|
||
|
def download_file(filename):
|
||
|
"""处理文件下载"""
|
||
|
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
|
||
|
|
||
|
@app.route('/delete/<filename>', methods=['GET'])
|
||
|
def delete_file(filename):
|
||
|
"""处理文件删除"""
|
||
|
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||
|
if os.path.exists(file_path):
|
||
|
os.remove(file_path)
|
||
|
return redirect(url_for('index'))
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(debug=True)
|