简体   繁体   中英

Upload audio file to Parse REST API via Flask

I am trying to upload an audio recording to Parse via their REST API. I am uploading the blob to my Flask app and then converting that file to a base64 string to be sent to Parse.

The response from Parse is that the upload was successful, but the audio file doesn't play when browsing to the file on Parse.

Any help would be greatly appreciated.

file.wav is the audio recording in an uint8array

// js
ParleData.Upload = function(file) {
   var dataView = new DataView(file.wav.buffer);
   var blob = new Blob([dataView], { type: 'audio/x-wav' });

   var form = new FormData();
   form.append('file', blob, 'audio.wav');

   return $.ajax({
      type: 'POST',
      url: '/upload',
      contentType: false,
      processData:false,
      cache:false,
      data: form
   });
}

# views.py
@app.route('/upload', methods=['POST'])
def upload():
   wav = request.files['file']
   return jsonify(DAS.upload(wav))

# das.py
def upload(self, wav):
    b64 = base64.b64encode(wav.read())

    connection = httplib.HTTPSConnection('api.parse.com', 443)
    connection.connect();
    connection.request('POST', 'https://api.parse.com/1/files/audio', b64, {
    'X-Parse-Application-Id':self.keys.X_PARSE_APP_ID,
    'X-Parse-REST-API-Key':self.keys.X_PARSE_RESTAPI_ID,
    'Content-Type': "audio/x-wav"
    })
    return json.loads(connection.getresponse().read())

The base64 encode step is unnecessary. The file can be posted without any transformation and should be playable. If you can use a streaming upload, this may improve the performance of the client also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM