我正在发送带有用户麦克风输入的blob,从角度到django api端点。 blob是一个8000Hz的单声道wav文件。
// Prepare data
var data = new FormData();
data.append('section_item_id', id);
data.append('blob', blob);
api观点:
def compare_audio(request):
print dict(request.data)
return Response({'status' : random.choice(['true', 'false'])})
我可以看到blob与该打印一起发送。出于测试目的,我想导出我在django中获得的blob,并将其保存到服务器上的文件中。怎么样?
答案 0 :(得分:0)
从数据中你可以使用scipy创建wav:
import numpy as np
import scipy.io.wavfile
import math
file_name="another.wav"
rate=8000
data2 = np.asarray(request.data, dtype=np.int16)
scipy.io.wavfile.write(file_name,rate,data2)
您可以使用mod_xsendfile使用apache从django视图提供文件,例如:
response = HttpResponse(mimetype='audio/wav')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['Accept-Ranges'] = 'bytes'
response['X-Sendfile'] = smart_str(path_to_file)
return response