implemented file write in download function