Please help me to understand file upload #2712
Answered
by
krishnaTORQUE
krishnaTORQUE
asked this question in
Q&A
-
use actix_multipart::Multipart;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use futures::stream::StreamExt;
// use std::fs::File;
use std::{io, str};
while let Some(item) = payload.next().await {
let field = item.unwrap();
let content_type = field.content_disposition();
let filename = content_type.get_filename().unwrap();
// let mut file = File::create(format!("pub/{}", filename)).unwrap();
// file.write_all(parts[0].data().await.unwrap().unwrap().bytes())
// .unwrap();
println!("content_type: {}", content_type);
println!("file name: {}", filename);
// while let Some(chunk) = field.next().await {
// println!("-- CHUNK: \n{:?}", str::from_utf8(&chunk.unwrap()));
// }
}
HttpResponse::Ok().body("Done") This above code is working fine. |
Beta Was this translation helpful? Give feedback.
Answered by
krishnaTORQUE
Mar 24, 2022
Replies: 1 comment 3 replies
-
I got the solution while let Some(item) = payload.next().await {
let mut field = item.unwrap();
let content_type = field.content_disposition();
println!("{}", field.name());
println!("CONTENT TYPE: {}", content_type);
println!("FILE NAME: {}", content_type.get_filename().unwrap());
println!("UPLOAD NAME FIELD: {}", content_type.get_name().unwrap());
println!("MIME TYPE: {}", field.content_type());
println!("FIELD: {:?}", field);
let file_path = format!("pub/{:?}.jpg", time::Instant::now());
let mut create_file = File::create(file_path).unwrap();
while let Some(chunk) = field.next().await {
let data = chunk.unwrap();
create_file.write_all(&data).unwrap();
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
krishnaTORQUE
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got the solution