Skip to content

download a text file from a website to my local filesystem? #2759

Discussion options

You must be logged in to vote

For reference, since some of the types changed.

This is the maud snippet;

html!(a href="/download" download { button { "Download" }})

Include this in your router, which is what htmx calls when you click the button;

        .route("/download", get(download))

This is the function with the new types;

async fn download() -> impl IntoResponse {
    let file = match tokio::fs::File::open("download_file.txt").await {
        Ok(file) => file,
        Err(err) => return Err((StatusCode::NOT_FOUND, format!("File not found: {}", err))),
    };
    let stream = ReaderStream::new(file);
    let body = Body::from_stream(stream);

    let mut headers = HeaderMap::new();
    headers.insert(
        header

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@jplatte
Comment options

Answer selected by jplatte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants