Fix buffering problem of Tempfile and return type mismatch