欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > share.py

share.py

2025/2/22 2:03:23 来源:https://blog.csdn.net/weixin_60092693/article/details/145664991  浏览:    关键词:share.py
share_project/
├── share.py
└── templates/└── index.html
# save_as app.py
import os
from flask import Flask, render_template, request, send_from_directory, redirect, url_forapp = Flask(__name__)# 使用Windows路径格式,此处设置为D盘根目录下的FileStorage文件夹
UPLOAD_FOLDER = r'D:\FileStorage'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024  # 最大上传100MB# 创建存储目录(如果不存在)
if not os.path.exists(UPLOAD_FOLDER):os.makedirs(UPLOAD_FOLDER)@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'POST':# 检查上传请求if 'file' not in request.files:return redirect(request.url)file = request.files['file']if file.filename == '':return redirect(request.url)if file:# 安全保存文件filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)file.save(filename)return redirect(url_for('index'))# 获取文件列表(排除子目录)files = [f for f in os.listdir(app.config['UPLOAD_FOLDER'])if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], f))]return render_template('index.html', files=files)@app.route('/download/<path:filename>')
def download(filename):# Windows安全路径检查safe_path = os.path.abspath(app.config['UPLOAD_FOLDER'])requested_path = os.path.abspath(os.path.join(app.config['UPLOAD_FOLDER'], filename))if not requested_path.startswith(safe_path):return "非法访问路径", 403return send_from_directory(app.config['UPLOAD_FOLDER'],filename,as_attachment=True)if __name__ == '__main__':# 设置为局域网可访问(可选)app.run(host='0.0.0.0', port=5000, debug=True)
<!DOCTYPE html>
<html>
<head><title>Windows文件传输服务</title><style>body { font-family: Segoe UI, sans-serif; max-width: 800px; margin: 20px auto; }.container { padding: 20px; border: 1px solid #ddd; }ul { list-style-type: none; padding: 0; }li { padding: 8px; border-bottom: 1px solid #eee; }a { color: #0066cc; text-decoration: none; }a:hover { text-decoration: underline; }</style>
</head>
<body><div class="container"><h1>📁 文件传输服务</h1><h2>⬆ 上传文件</h2><form method="post" enctype="multipart/form-data"><input type="file" name="file" style="padding: 8px;"><input type="submit" value="上传" style="padding: 8px 20px; background: #0066cc; color: white; border: none; cursor: pointer;"></form><h2>📋 文件列表(存储路径:{{ UPLOAD_FOLDER }})</h2><ul>{% for file in files %}<li><span style="width: 70%; display: inline-block;">{{ file }}</span><a href="{{ url_for('download', filename=file) }}">⬇ 下载</a></li>{% endfor %}</ul></div>
</body>
</html>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词