【发布时间】:2020-03-09 14:24:13
【问题描述】:
我正在尝试使用 JavaScript 录制音频并将其发送到 Python 服务器。至于现在,我已经运行它并且记录工作正常。使用AJAX发布和获取数据,我正在尝试传递它,但是发送到Python不起作用。
这是我的代码:
JavaScript:
function submit(blob) {
var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var fd = new FormData();
base64data = reader.result;
console.log(base64data);
fd.append('file', base64data, 'audio.ogg');
$.ajax({
type: 'POST',
url: '/',
data: fd,
cache: false,
processData: false,
contentType: false,
enctype: 'multipart/form-data'
}).done(function(data) {
console.log(data);
});
}
Python 服务器:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html")
@app.route('/', methods=['POST', 'GET'])
def get_data():
with open('./audio.ogg', 'wb') as f:
f.write(request.data)
f.close()
print("FILE CLOSED")
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True)
【问题讨论】:
标签: javascript python ajax server