How to interact with files on the server? #1547
-
Hello guys. I have a question (2 in fact). Suppose i want to add a custom font for my website (Server side rendering is enabled):
@font-face {
font-family: SomeFont;
src: url(/assets/fonts/SomeFont.woff2);
}
#[server(GetFile, "/assets/fonts")]
pub async fn get_file() -> Result<Vec<u8>, ServerFnError> {
let f = std::fs::read("./public/assets/fonts/SomeFont.woff2").unwrap();
Ok(f)
}
<Route path="/assets/fonts/:font_name" view=|cx| {
view! { cx, {
let font_file_resource = create_resource(cx, || (), |_| async move { get_file(font_name).await });
move || match font_file_resource.read(cx) {
None => view! { cx, }.into_view(cx),
Some(data) => view! { cx, { move || print!("{:?}", data.unwrap()) } }.into_view(cx)
}
}}
}/> The problem is i don't know how to stream down the file to the client correctly. Should i do it like above where i create a resource, call get_file function in it and then read it's content so that when it's ready it prints down the content? In fact i think i should make a seperate API and routing on the axum server for this purpose... I know the above codes are really dumb and may not even compile and have a lot of mistakes... But i just wanted to show you my intention... So my main questions are: Question 1: When question 1 is solved i want to find the answer for question 2... Question 2: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is kind of a "wrong tool for the job" situation. Leptos is a UI framework. If you're trying to serve a static file, you should use the appropriate tool for serving static files for whichever server you're using. For Axum, for example, this would be Every one of the Axum examples and the Axum starter also include a handler for serving static files or a 404. Your question about uploading files is completely separate. The search term you want here is "multipart form upload." A quick search gave me a useful article. Question 3 (a progress bar) does touch on UI more. You can use the |
Beta Was this translation helpful? Give feedback.
This is kind of a "wrong tool for the job" situation. Leptos is a UI framework. If you're trying to serve a static file, you should use the appropriate tool for serving static files for whichever server you're using. For Axum, for example, this would be
tower_http::services::ServeFile
.Every one of the Axum examples and the Axum starter also include a handler for serving static files or a 404.
Your question about uploading files is completely separate. The search term you want here is "multipart form upload." A quick search gave me a useful article.
Question 3 (a progress bar) does touch on UI more. You can use the
<progress>
HTML element with a signal that tracks how much of the file upl…