BurntSushi / rust-csv

A CSV parser for Rust, with Serde support.
The Unlicense
1.69k stars 219 forks source link

Header Implementations "Content-Disposition". #348

Closed wnesbv closed 8 months ago

wnesbv commented 8 months ago
`

# python as an example
output = io.StringIO()
fle = csv.writer(output)
content = output.getvalue()
headers = {"Content-Disposition": "attachment;filename=as.csv"}
return Response(content, headers=headers)
`
`

// rust as an example
pub async fn import_users(State(pool): State<Pool>) -> impl IntoResponse {

let mut conn = pool.get().await.unwrap();
use schema::users::dsl::*;
let data = users
    .select(ListUser::as_select())
    .load(&mut conn)
    .await.unwrap();
let mut wtr = Writer::from_writer(vec![]);
for pat in data {
    wtr.serialize(pat).unwrap();
}
wtr.flush().unwrap();
return Response::builder()
    .status(StatusCode::OK)
    .header("Location", "/")
    .header("Content-Disposition", "attachment;filename=or.csv")
    .body(Body::from("out"))
    .unwrap()
}
`

.body(Body::from("..?")) I've been looking for solutions for a long time, I hope for help.

BurntSushi commented 8 months ago

I have absolutely no idea what you're asking.

Please read this: https://www.freecodecamp.org/news/how-to-ask-good-technical-questions/

wnesbv commented 8 months ago

"Note that the CSV writer is buffered automatically, so you should not wrap wtr in a buffered writer like io::BufWriter." Implement a buffer (CSV records) for the Response body as an option. Vec apply to the Response body.