Skip to content

Streaming file #192

Answered by chrislearn
Paul-Vandell asked this question in Q&A
Nov 30, 2022 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

Try this:

#[handler]
async fn hello(res: &mut Response) {
    let file = tokio::fs::File::open("Cargo.toml").await.unwrap();
    let stream = ReaderStream::new(file);

    res.add_header(header::CONTENT_TYPE, "text/toml; charset=utf-8", true);
    res.add_header(header::CONTENT_DISPOSITION,   "attachment; filename=\"Cargo.toml\"", true);
    res.streaming(stream);
}

Or you can just use NamedFiile.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Paul-Vandell
Comment options

@Paul-Vandell
Comment options

@chrislearn
Comment options

Answer selected by Paul-Vandell
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