在Nodejs中处理多部分post方法请求抛出意外的字段错误

时间:2016-09-04 18:32:50

标签: android node.js

我正在使用post从Android应用程序尝试Loopj Async Http Client方法请求,并使用使用Multer的节点服务器处理请求 使用下面的代码

步骤1

public static void post(String url, File entity, AsyncHttpResponseHandler responseHandler) {
            RequestParams params = new RequestParams();
        try {
            params.put("image",entity);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

getAbsoluteUrl返回实际的Url以发布请求

步骤2(节点服务器)

router.post('/upload_image', function(req, res) {
    console.log(req.files);
});

但是,在收到请求后,我的节点js服务器抛出了一些我无法理解的错误,经过几个小时的谷歌搜索和搜索其他选项后,我仍然无能为力解决问题。

问题在于:

POST /upload_image 500 1537.367 ms - 2057
Error: Unexpected field
    at makeError (/home/jkrishna/Documents/ProjectX/node_modules/multer/lib/make-error.js:12:13)
    at wrappedFileFilter (/home/jkrishna/Documents/ProjectX/node_modules/multer/index.js:39:19)
    at Busboy.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/lib/make-middleware.js:112:7)
    at Busboy.EventEmitter.emit (events.js:106:17)
    at Busboy.emit (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/lib/main.js:31:35)
    at PartStream.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/lib/types/multipart.js:213:13)
    at PartStream.EventEmitter.emit (events.js:95:17)
    at HeaderParser.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:51:16)
    at HeaderParser.EventEmitter.emit (events.js:95:17)
    at HeaderParser._finish (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:68:8)
    at SBMH.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:40:12)
    at SBMH.EventEmitter.emit (events.js:95:17)
    at SBMH._sbmh_feed (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:95:16)
    at SBMH.push (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:56:14)
    at HeaderParser.push (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:46:19)
    at Dicer._oninfo (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:197:25)

1 个答案:

答案 0 :(得分:1)

问题是您已配置multer以查找字段名称为'file'的文件,但在您的Android代码中,您发送的是名为'image'的字段。改变其中一个,它应该有效。