flask|flask web 实现文件上传 下载 浏览 编辑
功能展示
代码展示
app.yp
from flask import Flask, request, render_template, redirect, url_for, make_response ,escape, session, flash, g, current_app, abort, jsonify, send_file
import requests
from werkzeug.utils import secure_filename
import os,time, platform
from flask_wtf import Form
from wtforms import TextField
from shell import os_scrpict, harbor
import sqlite3
from pathlib import Path
from flask_sqlalchemy import SQLAlchemyapp = Flask(__name__)
#app.pyapp.secret_key = 'development key'
app.config['UPLOAD_FOLDER'] = 'upload/'filepath = Path('upload/')@app.route('/upload')
def upload_file():
return render_template('dir_view.html')@app.route('/uploader', methods = ['GET', 'POST'])
@app.route('/uploader//', methods = ['GET', 'POST'])
def uploader(filepath=' '):
if request.method == 'POST':
repath = 'upload/' + filepath + '/'
print(repath)
f = request.files['file']
#f.save(os.path.join(app.config['UPLOAD_FOLDER'],secure_filename(f.filename)))
f.save(os.path.join(repath, secure_filename(f.filename)))
#return 'file uploaded successfully'
return redirect('/scan/{tfilepath}'.format(tfilepath = filepath))@app.route('/download/', )
def download_file(fullname):
#current_app.logger.debug(fullname)
print(fullname)
return send_file(fullname)@app.route('/look/', )
def look_file(fullname):
f = open(fullname, encoding='UTF-8')
resp = make_response(f.read())
#resp = make_response(open(fullname, encoding='UTF-8').read())
resp.headers["Content-type"] = "application/json;
charset=UTF-8"
# f = open(fullname, "r+",encoding='UTF-8',newline="\n")
# str = f.read()
# fullname1 = str(fullname).replace('\\', '/')
# with open(fullname, "r+",encoding='UTF-8',newline="\n") as f:# 默认模式为‘r',只读模式
#contents = f.read()
f.close()
return resp@app.route('/scan/')
@app.route('/scan//', )
def index(ortherpath=''):
file_ele_list = list()
dir_ele_list = list()
#for f in (Path('upload/'+ ortherpath).iterdir()):
for f in (Path('upload') / Path(ortherpath)).iterdir():
#for f in os_scrpict.allfile('upload/'):
if f.is_file():
fullname = str(f).replace('\\', '/')
file_ele_list.append({'is_dir': 0, 'filesize': os.path.getsize(f) / 1000,
'last_modify_time': time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime(fullname))),
'look_url': url_for('look_file', fullname=fullname), 'download_url': url_for('download_file', fullname = fullname), 'fullname': fullname})
if f.is_dir():
fullname = str(f).replace('\\', '/')
dirname = fullname.split('/')
dir_ele_list.append({'is_dir': 1, 'filesize': 0,
'last_modify_time': time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime(fullname))), 'look_url': url_for('look_file', fullname = dirname[1]), 'download_url': url_for('index', ortherpath = dirname[1]), 'fullname': fullname})
#print(dirname[1],"########",dir_ele_list)
#print(fullname,"fullname####",fullname[1:])
print(dir_ele_list + file_ele_list)
repath = request.path.split('scan')
print(request.path,"#############request.path##########",repath[1])
if 'look' in request.path:
look_file1 = request.path.split('look/')
return render_template('dir_view.html', ele_list=dir_ele_list + file_ele_list,path1 = {"path1": repath[1]},conflook = {"conflook": look_file(look_file1[1])})
else:return render_template('dir_view.html', ele_list=dir_ele_list + file_ele_list, path1={"path1": repath[1]}, conflook = {"conflook": "hello kugou"})@app.route('/get_configfile/', )
def get_configfile(configname):
print(configname)
f = open(configname, "r+",encoding='UTF-8',newline="\n")
str = f.read()
f.close()
return render_template('get_configfile.html', conflook = {"conflook": str,"configname": configname})@app.route('/edit_configfile//', methods = ['GET', 'POST'])
def edit_configfile(configname):
if request.method == 'POST':
fileconfig = request.form.get('conftext')
print(configname)
print(fileconfig)
f = open(configname, 'w', encoding='utf8')
# 写入文件内容
f.write(fileconfig)
f.close()return redirect('/scan/')if __name__ == '__main__':
app.run()
#app.run(host="172.16.117.33",port=5888)
dir_view.html
Title - 锐客网 dir_path : {{ path1.path1 }}
名称
最后修改时间
文件大小
下载文件
操作
{% for ele in ele_list %}
{% if ele.is_dir %}
目录 {{ ele.fullname }}
{{ ele.last_modify_time }}
{{ ele.filesize }} M
进入 {{ ele.fullname }}
打开目录
{% else %}
{{ ele.fullname }}
{{ ele.last_modify_time }}
{{ ele.filesize }} Kb
下载 {{ ele.fullname }}
编辑
{% endif %}{#预览 {{ ele.fullname }} #}
{#{{ ele.last_modify_time }} #}
{#{{ ele.filesize }} M #}
{#下载 {{ ele.fullname }} #}
{% endfor %}
get_configfile.html
Title - 锐客网
Nginx_config
- 样式 模板 写的有点糙,就不给你们了
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树
- 私有化轻量级持续集成部署方案--03-部署web服务(下)
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM