【问题标题】:Uploading a small audio file from iOS app to a Python-Flask server将小音频文件从 iOS 应用程序上传到 Python-Flask 服务器
【发布时间】:2014-04-19 21:33:02
【问题描述】:

我确实浏览了与从 iOS 应用上传音频文件相关的每一篇 StackOverflow 帖子,但我的代码无法正常工作。

基本上,我正在创建一个可以录制声音的应用程序,然后将声音发送到 python-flask 服务器,该服务器在声音文件上运行一些机器学习算法并将值返回给客户端(iOS 应用程序)。我的录音部分正在工作,并且可以与服务器进行有效的通信,但是我无法将音频文件发送到服务器。

所以我的问题分为两部分: 1. 如何将音频文件从 iOS 发送到服务器? 2、如何在服务器中读取接收到的音频文件?

这是我的代码:

// Code to send an audio file to a local Python-Flask server (this code has been taken from a 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 your 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];

我的服务器代码是:

import os
from flask import Flask, request, redirect, url_for, jsonify
import uuid
from subprocess import Popen, PIPE
app = Flask(__name__)

# for ios
@app.route('/', methods=['POST'])
def classify():
    soundFile = request.file
    #run the algorithm on soundFile to get a particular value specific to the sound file
    return jsonify({'answer':str(value)})

if __name__ == '__main__':
    app.debug = True
    app.run()

目前,代码无法正常工作。我要么没有从客户端正确发送文件,要么没有在服务器端正确读取它。基本上,request.file 是空的。

感谢您的帮助!

【问题讨论】:

  • 你搞定了吗?

标签: ios audio flask http-post nsurlconnection


【解决方案1】:

很确定您要查找的是request.files['filename'],而不是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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2011-06-04
    • 2014-04-26
    • 1970-01-01
    相关资源
    最近更新 更多