Uploading a small audio file from iOS app to a Python-Flask server -


i have literally gone through each , every stackoverflow post related audio file upload ios app, haven't been able code working.

basically, creating app can record sound, sound sent python-flask server, runs machine learning algorithms on soundfile , returns value client(ios app). have recording part working, , have been ale communicate server having trouble sending audio file server.

so question of 2 parts: 1. how send audio file ios server? 2. how read received audio file in server?

here codes:

// code send audio file local python-flask server (this code has been taken few answers on stack overflow)  nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request sethttpmethod:@"post"]; [request seturl:[nsurl urlwithstring:@"http://127.0.0.1:5000"]]; nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"];  // audio data nsmutabledata *postdata = [nsmutabledata data]; nsstring *header = [nsstring stringwithformat:@"--%@\r\n", boundary]; [postdata appenddata:[header datausingencoding:nsutf8stringencoding]]; //add filename entry nsstring *contentdisposition = [nsstring stringwithformat:@"content-disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", @"filename", @"sound.wav"]; [postdata appenddata:[contentdisposition datausingencoding:nsutf8stringencoding]]; [postdata appenddata:[nsdata datawithcontentsoffile:@"/users/talhajansari/library/application support/iphone simulator/7.1/applications/e1061366-05c2-4412-b381-39023f3c6c18/documents/sound.wav"]]; nsstring *enditemboundary = [nsstring stringwithformat:@"\r\n--%@\r\n",boundary]; [postdata appenddata:[enditemboundary datausingencoding:nsutf8stringencoding]]; [request sethttpbody:postdata];  [request settimeoutinterval:30.0];  nsurlconnection *conn = [[nsurlconnection alloc] initwithrequest:request delegate:self]; 

and code server is:

import os flask import flask, request, redirect, url_for, jsonify import uuid subprocess import popen, pipe app = flask(__name__)  # ios @app.route('/', methods=['post']) def classify():     soundfile = request.file     #run algorithm on soundfile particular value specific sound file     return jsonify({'answer':str(value)})  if __name__ == '__main__':     app.debug = true     app.run() 

for now, code not working. either not sending file correctly client side, or not reading on server side. basically, request.file empty.

thank help!

pretty sure it's request.files['filename'] you're looking for, not request.file. flask's docs:

def upload_file():     if request.method == 'post':         file = request.files['file']         if file:             filename = secure_filename(file.filename)             file.save(os.path.join(app.config['upload_folder'], filename))             return redirect(url_for('uploaded_file',                                     filename=filename)) 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -