upload file before closing it
wrap writing/closing of tempfile in ensure block
unlink tempfile after it is no longer needed