通过XHR从客户端发送blob数据到NodeJS服务器
我想从JA客户端发送一个blob数据到NodeJS服务器。
现在我有这个客户端代码:[…] recorder.exportWAV(function(blob){audioObj = blob;});
var formData = new FormData(); formData.append("operation", "addMessage"); formData.append("msg", audioObj); var xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8080/sendRecord", true); xhr.onreadystatechange = function(){ if(xhr.readyState==4 && xhr.status==200){ // get response from server } } xhr.send(formData);
我使用一些JS库来logging来自集成麦克风的audio,最后我有一个audio对象是一个blob对象。
而这个si node.js服务器,我想调整接收数据。
var http = require('http'), url = require('url'); var app = http.createServer(function(request, response){ var path = url.parse(request.url).pathname; // check the url path if (path == "/sendRecord") { // check the request method if(request.method != "POST"){ // return error } // _______________________________ //| here I want catching XHR data | // ––––––––––––––––––––––––––––––– } }).listen(8080); console.log("Server running on: http://localhost:8080/");
我认为应该是一些服务器端的XHR API来响应客户端的XHR请求,但我不知道在哪里find他们。 任何人都可以帮我吗? 也白衣链接或文件…;)
谢谢!
我回答自己…如果有人发现有用的将来;)
使用Formidable( https://github.com/felixge/node-formidable )我已经这样做了:
var http = require('http'), url = require('url'), formidable = require('formidable') util = require('util'); var server = http.createServer().listen(8080); server.on('request', function(request, response) { var form = new formidable.IncomingForm(), fields = [], files = []; form.on('error', function(err){ response.writeHead(200, {'content-type': 'text/plain'}); response.end('error:\n\n' + util.inspect(err)); }); form.on('field', function(field, value){ console.log(field, value); fields.push([field, value]); }); form.on('file', function(field, file){ console.log(field, file); files.push([field, file]); }); form.on('end', function(){ console.log('-> upload done'); response.writeHead(200, {'content-type': 'text/plain'}); response.write('Received fields:\n\n ' + util.inspect(fields)); response.write('\n\n'); response.end('received files:\n\n ' + util.inspect(files)); }); form.encoding = 'utf-8'; form.uploadDir = './tmp'; form.keepExtensions = true; form.parse(request); });
感谢robertklep的build议!
- 如何从模块返回Node.js中的数组
- Javascriptrecursion包装儿童(matryoshka娃娃)
- 使用websocket和nodejs时,文件描述符的编号会增加
- 在Node.js中如何将multipart / form-data上传的图像转换为base64格式
- 为什么不把这个“限制”到周围的范围呢?
- 使用Webstormdebugging器和node.js
- 使用router.js中的promise来模拟error handling
- 是.hasOwnProperty()在Node.js for..in所必需的?
- 有没有办法find一个匹配两个不同的填充文件,并在findOne()中获取他的文档?