Consolidated generic media upload code into uploadMedia().