Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file browser support #39

Open
databasedav opened this issue Nov 18, 2021 · 5 comments
Open

file browser support #39

databasedav opened this issue Nov 18, 2021 · 5 comments

Comments

@databasedav
Copy link
Contributor

i am thinking this should be a special button that opens the host's file browser UI and allows files to be selected as one would expect, once the files are selected and confirmed, the bytes should end up in a variable somewhere in Zoon (not sure how it's api should look, perhaps passing a mutable to the button or something)

@MartinKavik
Copy link
Member

MartinKavik commented Nov 18, 2021

  • And there should be a way to upload the file(s) to the Moon app through Connection.
  • Also drag&drop would be nice as an alternative to the button.

@databasedav
Copy link
Contributor Author

any update on this? looks like these api's https://developer.mozilla.org/en-US/docs/Web/API/File_API/Using_files_from_web_applications need zoonic wrapping

in addition to the above features, it would be great to easily be able to expose a live preview of the uploaded file (e.g. showing the image)

@MartinKavik
Copy link
Member

any update on this?

I'll need it in both production MZ apps I'm writing but file uploading hasn't been priority yet. I'm keeping it my mind, I'll update the issue once there is something ready.

@databasedav
Copy link
Contributor Author

databasedav commented Sep 24, 2022

this actually ended up being pretty simple to do with just web-sys, gloo-file, and gloo-net

first, modify the type attr of a TextInput and add an on change listener which places the selected file in a Mutable file

TextInput::new()
.label_hidden("")
.update_raw_el(|raw_el| {
    let dom_element = raw_el.dom_element();
    raw_el
    .attr("type", "file")
    .style("white-space", "nowrap")
    .event_handler(clone!((file) move |_: events::Input| {
        if let Some(files) = dom_element.files() {
            let files: gloo_file::FileList = files.into();
            if let Some(f) = files.first() {
                file.set(Some(SendWrapper::new(f.clone())));
            }
        }
    }))
})

then use gloo_file::futures to extract the data into your desired format, for example, u can get it into a data:image format to pass to Image.url(...) with this

if let Some(f) = file.get_cloned() {
    if let Ok(url) = gloo_file::futures::read_as_data_url(&*f).await {
        url ...
    }
}

finally, send it to your api with web_sys::FormData and gloo_net::http::Request

if let Some(f) = file.get_cloned() {
    let form_data = web_sys::FormData::new()?;
    form_data.set_with_blob("file", f.as_ref())?;
    gloo_net::http::Request::post(endpoint).body(form_data).send().await?
}

@qtisan
Copy link

qtisan commented Aug 15, 2023

Hi, @MartinKavik , why not make tailwindcss integrated? A new styling solution would be long time for production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants