rustiron

Access to file path on form upload with Iron Params


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.


Solution

  • 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");
      }
    }