主页 > 开源代码  > 

share.py

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_for app = 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 "非法访问路径", 403 return 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>
标签:

share.py由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“share.py