How to access an uploaded image? #2273
-
QuestionDear Niceguy developers / experts, I discovered niceguy only 10 days ago and find it really awesome. I managed to design a webpage faster than I could ever imagine. And it is a lot of fun :-). I am however facing an obstacle with the image upload. I followed the recipe https://nicegui.io/documentation/section_controls#file_upload Many thanks for your help, Benjamin, Paris PS: I had the impression that #352 could provide me some hints but it did not work for me :-(. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
In |
Beta Was this translation helpful? Give feedback.
-
|
Many thanks Rodja. For the completness, I tested the following script from nicegui import events, ui
def handle_upload(e: events.UploadEventArguments):
with open(f"UploadedFiles/{e.name}", "wb") as f:
f.write(e.content.read())
ui.upload(on_upload=handle_upload).props('accept=.png').classes('max-w-full')
ui.run(host="127.0.0.1", port=8082)and it worked nicely :-). |
Beta Was this translation helpful? Give feedback.
-
|
Hello, Apologizes for not being clear enough.
Indeed, I was missing the e:lambda trick. As it may interest other persons, I am copying my working version: from nicegui import events, ui
def handle_upload(e: events.UploadEventArguments, file):
with open(f"UploadedFiles/{e.name}", "wb") as f:
f.write(e.content.read())
file["name"] = e.name
f = {"name": "Unknown"}
lab = ui.label()
lab.bind_text(f, "name")
ui.upload(on_upload=lambda e: handle_upload(e, f)).classes('max-w-full')
ui.run(host="127.0.0.1", port=8082)
This is quite an awkward question / idea. I found a way to do this in two steps that is much more natural to the user. Thanks again for your prompt feedback. I am closing the discussion as everything is now clear. Cheers |
Beta Was this translation helpful? Give feedback.
In
ui.uploadyou can register a function which is executed for every uploaded file with theon_uploadparameter. For the called function a parameter of typenicegui.events.UploadEventArgumentsis provided. See https://nicegui.io/documentation/upload#show_file_content for a demo where the content of the uploaded file is shown in a dialog. You could also write this content to a file of your choice if you need to store the uploaded data.