2016-04-04 22 views
1

我想從JS發送一個音頻blob到python腳本(它運行在服務器上)。 我的JS ajax ..看起來像這樣。如何將JavaScript的音頻blob發送到python?

var fileType = 'audio'; 
var fileName = 'output.wav'; 
var formData = new FormData(); 
formData.append(fileType + '-filename', fileName); 
formData.append(fileType + '-blob', blob); 
    $.ajax({ 
    type: 'POST', 
    url: 'http://localhost/python/audio.py', 
    data: {audio:formData}, 
     success: function(response) { 
     alert(respose); 
    } 
    }); 

和我的python腳本看起來像這樣。

#!/usr/bin/python3 
print("Content-Type: text/html") 
print() 
import ssl 
import cgi 
import wave 
import contextlib 
form = cgi.FieldStorage() 
fname = form.getvalue("audio", "error") 
with contextlib.closing(wave.open(fname,'r')) as f: 
    frames = f.getnframes() 
    rate = f.getframerate() 
    duration = frames/float(rate) 
    print(duration) 

現在,我只是測試,所以它應該讓我的音頻文件的持續時間。斑點是通過record.js

這是行不通的,因爲python無法識別該文件。任何解決方案

PS:我使用Xampp Server,在本地主機上運行。

針對WOJTEK Marczenko:錯誤是

[Mon Apr 04 18:26:09.537912 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: Traceback (most recent call last):: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.537978 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/home/shashank/project/dutchman/python/audio.py", line 10, in <module>: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538002 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:  with contextlib.closing(wave.open(fname,'r')) as f:: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538024 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/wave.py", line 499, in open: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538036 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:  return Wave_read(f): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538056 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/wave.py", line 163, in __init__: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538065 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:  self.initfp(f): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538086 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/wave.py", line 128, in initfp: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538097 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:  self._file = Chunk(file, bigendian = 0): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538110 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: File "/usr/lib/python3.5/chunk.py", line 61, in __init__: /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538119 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215:  self.chunkname = file.read(4): /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
[Mon Apr 04 18:26:09.538132 2016] [cgi:error] [pid 5330] [client ::1:60802] AH01215: AttributeError: 'NoneType' object has no attribute 'read': /home/shashank/project/dutchman/python/audio.py, referer: http://localhost/index.html 
+0

在哪一點它「不工作」? python是否接收音頻表單數據?你也在嘗試alert()時輸入'response'錯誤() –

+0

好吧,似乎有一些問題,ajax導致「jquery.min.js:2846 Uncaught TypeError:非法調用」。就好像我註釋掉ajax,錯誤似乎消失了。 –

+0

我傾向於使用谷歌Chrome的開發人員工具來查看實際上從您的ajax/javascript表單發送的數據。 –

回答

2

它看起來就像你不能正常發送BLOB作爲表單字段。將Blob附加到FormData的正確方法是formData.append(fileType, blob, fileName);。你也應該重視只是FORMDATA,而不是在另一個對象嵌套它:

var formData = new FormData(); 
formData.append(fileType, blob, fileName); 
$.ajax({ 
    type: 'POST', 
    url: 'http://localhost/python/audio.py', 
    data: formData, 
    processData: false, // prevent jQuery from converting the data 
    contentType: false, // prevent jQuery from overriding content type 
    success: function(response) { 
     alert(response); 
    } 
}); 

來源: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append http://www.mattlunn.me.uk/blog/2012/05/sending-formdata-with-jquery-ajax/

在Python端,您需要使用CGI模塊根據Python文檔(不能發佈更多的鏈接)。我相信正確的方法是這樣的:

form = cgi.FieldStorage() 
fname = form["audio"].filename 
print "Got filename:", fname # in case of problems see if this looks ok 

with contextlib.closing(wave.open(fname,'r')) as f: 
    ... 
+0

我如何在python中恢復這個文件?似乎仍然存在一些問題。我在添加有問題的錯誤,因爲評論似乎有字符限制 –

+0

更新了答案,如果您搜索,您還可以在文檔中看到示例(https://docs.python.org/2/library/cgi.html)爲'文件名' –

相關問題