【发布时间】:2015-08-27 21:34:33
【问题描述】:
我正在尝试将音频文件从我的 iOS 应用程序上传到我的烧瓶后端。 POST 请求通过,但我收到一条错误消息,提示“浏览器(或代理)发送了此服务器无法理解的请求。”
我正在查看烧瓶文档来执行此操作,但我看不出他们在做什么不同。
@auth.route('/uploadfile', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
print('post request received')
file = request.files['file']
if file and allowed_file(file.filename):
print('file name is valid and saving')
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify({'success': 1})
else:
print('file failed to save')
return jsonify({'success': 2})
def allowed_file(filename):
print('in allowed_file')
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['AllOWED_EXTENSIONS']
//Config settings
UPLOAD_FOLDER = '/Users/Michael/Desktop/uploads'
ALLOWED_EXTENSIONS = set(['m4a'])
iOS 端
func savePressed(){
var stringVersion = recordingURL?.path
let encodedSound = NSFileManager.defaultManager().contentsAtPath(stringVersion!)
let encodedBase64Sound = encodedSound!.base64EncodedStringWithOptions(nil)
let dict = ["file": encodedBase64Sound]
let urlString = "http://127.0.0.1:5000/auth/uploadfile"
var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "Post"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(dict as NSDictionary, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//Completion handler
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
【问题讨论】: