filetype.py
filetype.py copied to clipboard
Check file type from request data
I am trying to get the file type from request data and save it, but the saved file can't use if I call filetype.guess( audio_file ).
I also try to save the data first, read the saved file and save again after i checked the file type, the saved file can't use too.
how to I solved this problem?
- get the file type from request data and save it
audio_file = request.files['data']
kind = filetype.guess( audio_file )
if kind is not None :
file_type = kind.extension
wav_path = os.path.join( current_app.config['UPLOAD_FOLDER'], 'audio.'+file_type )
- save the data first, read it and save again
audio_file = request.files['data']
tmp_path = os.path.join( current_app.config['UPLOAD_FOLDER'], "upload_audio.tmp" )
audio_file.save( tmp_path )
tmp_file = open( tmp_path , 'rb' )
tmp_data = tmp_file.read()
kind = filetype.guess( tmp_data )
tmp_file.close()
if kind is not None :
file_type = kind.extension
wav_path = os.path.join( current_app.config['UPLOAD_FOLDER'], wav_id+'.'+file_type )
wav_file = open( wav_path , 'wb' )
wav_file.write( tmp_data )
wav_file.close()
@Winedays Do you have an example url? I did not get your problem. Are you unable to check the file type or unable to play the audio file?
Have you had a look here? https://github.com/h2non/filetype.py/issues/62#issuecomment-619270128