I am using this code to loop over my Multipart, which is working fine:
// Variable to save file in
let mut file: Option<actix_multipart::Field> = None;
while let Some(item) = payload.next().await {
let mut field = item?;
// things
//,.,
// (Pseudo code: If field's name is 'File', save in variable file)
if field.name == "file" {
file = Some(field); // THIS LINE MAKES THE CODE STOP
}
}
println!("THIS IS NOT PRINTING BECAUSE CODE IS STOPPED");
// Wanted to save file to do this (custom function)
let upload_status = files::save_file(file, &file_name).await;
but my code stops on a specific line.
I am looping over my multipart and reading all the field's names to map the data of the fields to the correct place in my code. When I reach the multipart's field containing the file. I want to save that Field, and pass the Field to a function I've written to save the file
Why does this file = Some(field); stop my code? It's not exiting, it's just lagging/waiting for something.
It sounds like you are misunderstanding what the code is doing. There's no reason for it to stop at that particular line. In particular, your code probably had another iteration of the while loop and its waiting for payload.next().await. Alternatively, the code you posted is different from your real code in some other important way.
You were right. Manually breaking my loop after file = Some(field); makes the code continue.
But what I don't understand is that in my function save_file I now get the error:
thread 'actix-rt:worker:0' panicked at 'called `Option::unwrap()` on a `None` value', /Users/niel/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-multipart-0.3.0/src/server.rs:427:40
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
with this block of code:
while let Some(chunk) = field.next().await {
let data = chunk.unwrap();
// filesystem operations are blocking, we have to use threadpool
f = web::block(move || f.write_all(&data).map(|_| f))
.await
.unwrap();
}
while let Some(chunk) = field.next().await {
let data = chunk.unwrap();
// filesystem operations are blocking, we have to use threadpool
f = web::block(move || f.write_all(&data).map(|_| f))
.await
.unwrap();
}
How would I log that?
println!("field: {:?}", field); Just gives back a the correct file/image-field.
And it ain't the unwrap in my while-loop I guess. But the while-loop itself.
This (including comments):
while let Some(chunk) = field.next().await {
//let data = chunk.unwrap();
// filesystem operations are blocking, we have to use threadpool
/* f = web::block(move || f.write_all(&data).map(|_| f))
.await
.unwrap();*/
}
also gives ```thread 'actix-rt:worker:0' panicked at 'called Option::unwrap() on a None value', /Users/niel/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-multipart-0.3.0/src/server.rs:427:40
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace