{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 文件上传\n", "\n", "`File Upload` 基于 Quasar 的[QUploader组件](https://quasar.dev/vue-components/uploader)。\n", "\n", "- `multiple`: 允许一次上传多个文件(默认:`False`)\n", "- `max_file_size`: 文件的最大大小,以字节为单位(默认:`0`)\n", "- `max_total_size`: 所有文件的最大总大小,以字节为单位(默认:`0`)\n", "- `max_files`: 最大文件数量(默认:`0`)\n", "- `on_upload`: 每个上传文件时执行的回调函数(类型:`nicegui.events.UploadEventArguments`)\n", "- `on_rejected`: 每个被拒绝的文件执行的回调函数\n", "- `label`: 上传器的标签(默认:`''`)\n", "- `auto_upload`: 当文件被选择时自动上传(默认:`False`)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.upload(on_upload=lambda e: ui.notify(f'Uploaded {e.name}')).classes('max-w-full')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 限制上传 \n", "\n", "在这个示例中,上传被限制为最大文件大小1MB。当文件被拒绝时,会显示一个通知。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import ui\n", "\n", "ui.upload(on_upload=lambda e: ui.notify(f'Uploaded {e.name}'),\n", " on_rejected=lambda: ui.notify('Rejected!'),\n", " max_file_size=1_000_000).classes('max-w-full')\n", "\n", "# ui.run()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 显示内容\n", "\n", "在这个示例中,上传的Markdown文件会在一个对话框中显示。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from nicegui import events, ui\n", "\n", "with ui.dialog().props('full-width') as dialog:\n", " with ui.card():\n", " content = ui.markdown()\n", "\n", "def handle_upload(e: events.UploadEventArguments):\n", " text = e.content.read().decode('utf-8')\n", " content.set_content(text)\n", " dialog.open()\n", "\n", "ui.upload(on_upload=handle_upload).props('accept=.md').classes('max-w-full')\n", "\n", "# ui.run()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }