how to use ui.upload component to upload files to server #5320
Replies: 3 comments 2 replies
|
Hello @wasabismile, thanks for giving NiceGUI a try! Consider https://nicegui.io/documentation/upload Note:
You can use NiceGUI stuff inside the latter only, and |
|
Hi @wasabismile, Like @evnchn mentioned, the documentation should give an idea how to handle uploads, especially https://nicegui.io/documentation/upload#show_file_content. You probably don't have to define your own By the way: Your NiceGUI version 1.4.47 is pretty old (actually 1.4.47 doesn't exist, maybe you mean 1.4.37?). I strongly recommend upgrading to get new features and improved performance. |
|
@falkoschindler @evnchn my NiceGUI version is 1.4.37 . I made a writing mistake and realized that I can't revise the version after submitting. I checked the code of the from fastapi import UploadFile, File
from pathlib import Path
from nicegui import ui,app,events
from fastapi import Request
from typing import cast
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
from typing import List
@app.post("/upload")
async def upload_files(request: Request):
#print(request.headers)
form = await request.form()
uploads = [cast(UploadFile, data) for data in form.values()]
for file in uploads:
file_path = UPLOAD_DIR / file.filename
print(file_path)
with open(file_path, "wb") as f:
f.write(await file.read())
return {"uploaded": 'ok'}
async def handle_upload(e: events.UploadEventArguments):
filename = e.name
file_path = UPLOAD_DIR / filename
print(file_path)
with open(file_path, "wb") as f:
f.write(e.content.read())
@ui.page("/")
def upload_page():
ui.upload().props('url="/upload"')
ui.upload(on_upload=handle_upload)
ui.run() |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Example Code
Description
I want to implement a feature that allows users to upload files to the server, but I don't know how to do it.
NiceGUI Version
1.4.47
Python Version
3.11
Browser
Chrome
Operating System
Windows
Additional Context
No response
All reactions