什么是node.js服务器端代码来处理来自phonegap ft.upload传入的请求?

我已经部分开发了一个混合Android应用程序使用node.js为我的服务器和JavaScript,jquery,phonegap(现在称为cordova1.6.0)的应用程序。

部分应用程序允许用户使用智能手机相机拍照,然后将其上传到服务器。

我在应用程序上传图片的代码是…

var imageURI = document.getElementById('display_image').src; if (!imageURI) { // || (self.elem.image_play.style.display == "none")) { console.log('no image uri defined - ' + JSON.stringify(imageURI)); //document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first."; return; } // Verify server has been entered server = 'http://192.168.1.3:8180/newimage/'; if (server) { console.log("starting upload"); // Specify transfer options var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); options.mimeType="image/jpg"; options.chunkedMode = false; // Transfer picture to server var ft = new FileTransfer(); ft.upload(imageURI, server, function(r) { console.log("upload successful"+r.bytesSent+" bytes uploaded."); //document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded."; }, function(error) { console.log("upload failed - Error Code = "+error.code); //document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code; }, options); } 

这工作正常,但我不知道如何处理node.js服务器上的图像。 目前我的代码是:

 '/newimage/': function(req,res){ var chunks; req.addListener('data', function (chunk) { console.log('in data processing'); chunks.push(chunk); }); req.addListener('end', function () { console.log('completing data processing'); fs.writeFile("out.jpg", chunks, function(err) { console.log(err); }); }); } 

此服务器方法执行,但是console.log行都不执行。

我一直在尝试2天来解决这个问题,我已经尝试了很多不同的方式,但我不知道如何处理来自phonegap ft.upload方法的传入图像文件。 任何人都可以帮忙吗?

一旦我有这个sorting,我的最终目的是上传图像文件到amazon s3存储,我想我可以使用knox模块。

我使用节点formidable和fs模块将上传的图像写入nodejs服务器上的/ tmp文件夹,以便稍后进行OCR。 代码基于www.nodebeginner.org书籍的图片上传示例。

我的JS代码在uploadPicture()方法中的phonegap项目中:

  // Specify transfer options var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1); options.mimeType="image/jpeg" options.chunkedMode = false; // Transfer picture to server var ft = new FileTransfer(); ft.upload(imageURI, server, function(r) { document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded."; }, function(error) { document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code; }, options, true); 

我在节点服务器上的requesthandler.js中的代码:

 function upload(response, request) { console.log("Request handler 'upload' was called."); var form = new formidable.IncomingForm(); form.parse(request, function(error, fields, files) { //logs the file information console.log(JSON.stringify(files)) fs.rename(files.file.path, "/tmp/test.png", function(err) { if (err) { fs.unlink("/tmp/test.png"); fs.rename(files.file.path, "/tmp/test.png"); } }); response.writeHead(200, {"Content-Type": "text/html"}); response.write("received image:<br/>"); response.write("<img src='/show' />"); response.end(); }); }