python内网上传下载工具

python内网上传下载工具

利用python的nicegui写了一个内网下载工具,支持上传和下载。

这样的工具很多,但是利用这个模块可以用很少的代码做一个比较好看的页面。

上传的文件会存在当前目录下生成一个upload文件夹中;下载则是读取upload文件夹。

首先下载模块

python 复制代码
pip install nicegui

代码

python 复制代码
from nicegui import app, ui
import os

# 如果目录不存在,则创建
directory = f'./upload/'
if not os.path.exists(directory):
    os.makedirs(directory)

app.add_static_files('/upload', 'upload')

with ui.row():
    with ui.button(on_click=lambda: ui.open('/uploadfile')):
        ui.label('上传')
        ui.icon('upload').classes('rounded-full w-16 h-16 ml-4')

    with ui.button(on_click=lambda: ui.open('/downloadfile')):
        ui.label('下载')
        ui.icon('download').classes('rounded-full w-16 h-16 ml-4')


def save_file(content, filename):
    """保存文件"""
    try:
        # 完整的文件路径
        file_path = os.path.join(directory, filename)

        with open(file_path, 'wb') as f:  # 保存图片
            f.write(content)
            ui.notify(f'{filename} 保存成功', color='positive')
    except Exception as e:
        ui.notify(e)


def list_files_and_dirs(directory):
    file_list = []
    for root, dirs, files in os.walk(directory):
        for name in files:
            file_list.append(os.path.join(root, name))
    return file_list


@ui.page('/uploadfile')
async def upload_page():
    with ui.row():
        with ui.button(on_click=lambda: ui.open('/')):
            ui.icon('arrow_back')
        ui.label('文件上传').classes('text-h5')
        with ui.button(on_click=lambda: ui.open('/downloadfile')):
            ui.icon('download')
    ui.upload(on_upload=lambda e: save_file(e.content.read(), e.name),
              on_rejected=lambda: ui.notify('Rejected!')).classes('max-w-full')


@ui.page('/downloadfile')
async def upload_page():
    file = list_files_and_dirs(directory)
    with ui.row():
        with ui.button(on_click=lambda: ui.open('/')):
            ui.icon('arrow_back')
        ui.label('文件下载').classes('text-h5')
        with ui.button(on_click=lambda: ui.open('/uploadfile')):
            ui.icon('upload')

    for i in file:
        with ui.item(on_click=lambda: ui.notify('开始下载...')):
            with ui.item_section().props('avatar'):
                ui.icon('download')
            with ui.item_section():
                ui.link(f'{i.replace(directory, "")}', i)


ui.run(title='文件上传下载工具', host='0.0.0.0', port=8080)
相关推荐
Tiger_shl5 分钟前
C# 预处理指令 (# 指令) 详解
开发语言·c#
(●—●)橘子……15 分钟前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
@Kerry~36 分钟前
phpstudy .htaccess 文件内容
java·开发语言·前端
CRMEB系统商城37 分钟前
CRMEB多商户系统(PHP)v3.3正式发布,同城配送上线[特殊字符]
java·开发语言·小程序·php
nueroamazing1 小时前
PPT-EA:PPT自动生成器
vue.js·python·语言模型·flask·大模型·项目·ppt
sali-tec1 小时前
C# 基于halcon的视觉工作流-章45-网格面划痕
开发语言·算法·计算机视觉·c#
一壶浊酒..1 小时前
python 爬取百度图片
开发语言·python·百度
机器视觉知识推荐、就业指导1 小时前
C语言中的预编译是什么?何时需要预编译?
c语言·开发语言
该用户已不存在1 小时前
工具用得好,Python写得妙,9个效率工具你值得拥有
后端·python·编程语言
·心猿意码·1 小时前
C++智能指针解析
开发语言·c++