云盘
This commit is contained in:
parent
d92e877313
commit
85b1dae296
46
yunpan/main.py
Normal file
46
yunpan/main.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
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)
|
27
yunpan/templates/index.html
Normal file
27
yunpan/templates/index.html
Normal file
|
@ -0,0 +1,27 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>简单云盘</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>文件管理</h1>
|
||||
<form action="/upload" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="file" required>
|
||||
<input type="submit" value="上传文件">
|
||||
</form>
|
||||
|
||||
<h2>文件列表</h2>
|
||||
<ul>
|
||||
{% for file in files %}
|
||||
<li>
|
||||
{{ file }}
|
||||
<a href="/download/{{ file }}">下载</a>
|
||||
<a href="/delete/{{ file }}" onclick="return confirm('确定删除?')">删除</a>
|
||||
</li>
|
||||
{% else %}
|
||||
<li>暂无文件</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user