I am developing API using Flask-restplus. One of the endpoints handles audio file uploads which can be either mp3
or wav
format. According to PUT request to upload a file not working in Flask, file uploaded by put
is in either request.data
or request.stream
. So this is what I did:
@ns.route('/upload')
class AudioUpload(Resource):
def put(self):
now = datetime.now()
filename = now.strftime("%Y%m%d_%H%M%S") + ".mp3"
cwd = os.getcwd()
filepath = os.path.join(cwd, filename)
with open(filepath, 'wb') as f:
f.write(request.stream.read())
return filepath
I am saving the file as mp3
. However sometime the file comes in as wav
. Is there a way to get the original file name from put request in the similar way as post
request:
file = request.files['file']
filename = file.filename