Axum - download file

Hello!
I would like create a service for downloading files by web browser.
I don't know how to create an HTTP response from bytes (file, Vec<u8>).
Please help me.

let app = Router::new() 
                 .route("/download/*key", get(download))
                 .with_state(state);

pub async fn download(state: State<AppState>, 
                      Path(path): Path<String>) -> impl IntoResponse
{
    let f: Vec<u8> = state.file.unwrap();
    /// TODO: create Response
}

This might help you: Example of how to return a file dynamically · tokio-rs/axum · Discussion #608 · GitHub

1 Like
pub async fn download(state: State<AppState>, 
                      Path(path): Path<String>) -> impl IntoResponse
{
    let data = b"your data";
    let stream = ReaderStream::new(&data[..]);
    let body = StreamBody::new(stream);
    let headers = [
        (header::CONTENT_TYPE, "text/toml; charset=utf-8"),
        (
            header::CONTENT_DISPOSITION,
            "attachment; filename=\"YourFileName.txt\"",
        ),
    ];
    (headers, body).into_response()
}