1

I tried to get the temporary path of an uploaded file using Iron params. I have this request handler:

fn handler(req: &mut Request) -> IronResult<Response> {
    let tmp_file_name = req.get_ref::<Params>().unwrap().find(&["file"]).unwrap();
    println!("{:?}", tmp_file_name);
    Ok( Response::with( (status::Ok, "Lorem Ipsum.") ) )
}

It displays something like this:

File { path: "/xxx/yyy", filename: Some("file.txt"), size: 123 }

But if I try to access to the path:

println!("{:?}", tmp_file_name.path());

It does not compile:

error: attempted access of field `path` on type `&params::Value`, 
but no field with that name was found

I think I missed some basics about type, but I don't know where to (re)start.

rap-2-h
  • 30,204
  • 37
  • 167
  • 263

1 Answers1

2

params::Value is not a params::File, but an enum that could contain a params::File.

This should work with proper imports (untested):

match req.get_ref::<Params>().unwrap().find(&["file"]) {
  Some(&Value::File(ref file)) => {
    println!("{:?}", file.path())
  }
  _ => {
    println!("no file");
  }
}
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • Oops it does not work "expected `&params::Value`, found `params::Value`" in "Some(Value::File(file))" – rap-2-h Jul 13 '16 at 10:27
  • Try `Some(Value::File(file))` -> `Some(&Value::File(file))`. – Dogbert Jul 13 '16 at 10:28
  • "cannot move out of borrowed content". It works with `Some(&Value::File(ref file))`. Anyway, thanks! – rap-2-h Jul 13 '16 at 10:33
  • Thanks for the edit! I wanted to confirm if it works before editing my answer as I don't have an Iron app setup locally to test myself. – Dogbert Jul 13 '16 at 10:45