0

I'd like to send image from android app to node.js server to save into MongoDB on server side. I use AsyncHttpClient to post request.

My code is like this:

Android ->

RequestParams param = new RequestParams();
param.put("email", email);
param.put("image", file, "image/jpg");

System.out.println("Param : " + param);
HttpClient.post("uploadImg_Profile/", param, new AsyncHttpResponseHandler() {

Node.js ->

app.js->
app.post('/uploadImg_Profile/', function(req, res){
    uploadImg_Profile.uploadImg_Profile(req, res);
})

uploadImg_Profile.js->

exports.uploadImg_Profile= function(req, res){
    var User = new user({ 
        email : req.body.email,
        img : req.body.image
    });
    //
    console.log("req : " + req);

    console.log("email : "+ User.email);
    console.log("image : " + User.img);

But console.log result is undefined. I respect that this is seen jhgdsfejdi734634jdhfdf like this BSON type result.

  1. How can I get img data?

  2. There is a way to get file's type from File object dynamically?

2 Answers2

0

You need to use the right type of body parser in your node.js code - from the result you are getting, it looks as if you're not interpreting it as a multipart form.

You need to register the middleware to use to interpret the POST, for example, using the multer parser:

app.js:

var multer = require('multer');

app.use(bodyparser.json());
app.use(multer({ inMemory: true, putSingleFilesInArray: true }));

app.post('/uploadImg_Profile/', function(req, res){
    uploadImg_Profile.uploadImg_Profile(req, res);
});

uploadImg_Profile.js:

exports.uploadImg_Profile= function(req, res){
    var User = new user({ 
        email : req.body.email,
        img : req.files['image'][0].buffer
    });

    console.log("req : " + req);

    console.log("email : "+ User.email);
    console.log("image : " + User.img);
}

Multer will also populate various other properties about the file, so you should be able to retrieve the type of the image from that using:

req.files['image'][0].mimetype

See the multer page on github for all the goodness.

EDIT: Added bodyparser.json as well as multer.

Mark Hughes
  • 7,264
  • 1
  • 33
  • 37
  • First of all, thank you for your reply. I used multer before another project. Did you mean user multer instead of express.bodyparser? When I get rid of app.use(express.bodyparser) and use app.use(multer(~~~)), It send a message that Cannot read property 'image' of undefined Please keep helping me.... – YoungHoon Kim May 28 '15 at 09:09
  • @YoungHoonKim yes, I believe the standard bodyparser can't do multipart form support so won't be able to interpret your file. You can choose for each route whether to use bodyparser or multer by passing the appropriate one - I'll add a second example to the answer to clarify using both. – Mark Hughes May 28 '15 at 09:12
  • Thanks for your added comment. Refer to your previous comment, I fix my code like below. and I get the same result. img output is undefined. var multer = require('multer'); app.use(multer({ inMemory: true, rename: function (fieldname, filename) { return filename; } })); exports.uploadImg_Profile= function(req, res){ var User = new user({ email : req.body.email, img : req.files['image'] }); // console.log("req : " + req); console.log("email : "+ User.email); console.log("image : " + User.img); – YoungHoon Kim May 28 '15 at 09:17
  • And when I use req.files['image'][0].buffer, Error fired, TypeError: Cannot read property 'buffer' of undefined – YoungHoon Kim May 28 '15 at 09:23
  • When I fix like your example, app.post('/uploadImg_Profile/', multer({inMemory: true, putSingleFilesInArray: true}), function(req, res){ uploadImg_Profile.uploadImg_Profile(req, res); }) this post function dosen't grap android request. uploadImg_profile function dosen't call. It mean that this request dosen't include multipart form data? – YoungHoon Kim May 28 '15 at 09:33
  • Can you edit the question to include a more complete copy of your node.js code? I'm struggling to understand the syntax you're using there. – Mark Hughes May 28 '15 at 09:52
  • @YoungHoonKim Updated - try that now? – Mark Hughes May 28 '15 at 10:05
  • I don't know what is different your answer from my code~ T_T – YoungHoon Kim May 28 '15 at 10:07
  • I'm passing multer on app.post in app.js instead of requiring it in uploadimage_profile.js, that should make it specific to this route. What is the outcome with the code as I've given in this answer? do you get a valid HTTP request/response? – Mark Hughes May 28 '15 at 10:13
  • sorry, I tried your new answer and then, it can't take '/uploadImg_Profile' request. uploadImg_Profile(req, res) function isn't called. I can get email when it out from email output. I think that request/response is working from this result. – YoungHoon Kim May 28 '15 at 10:14
  • Can you update the original question again with your original code (before adding multer)? Before you changed it, it was receiving the request correctly (but not parsing the image), right? – Mark Hughes May 28 '15 at 10:15
  • Okay, I will try. It is done. and this code works on email param. just dosen't works on image! Thanks a lot for your help.. – YoungHoon Kim May 28 '15 at 10:19
  • @YoungHoonKim you're welcome, I'm curious as to why that doesn't work, but I've updated again - try that (change in app.js to just app.use multer, this will use it for all requests but it should hopefully work!). – Mark Hughes May 28 '15 at 10:32
  • Mark, Do you have hang out id? Its so long conversation. – YoungHoon Kim May 28 '15 at 10:50
  • When I do this, get rid of bodyparser and put multer, It can`t get email property. T_T... and I put both bodyparser and multer, can't read buffer.. – YoungHoon Kim May 28 '15 at 11:38
  • Sorry, I don't know then I'm afraid! Hopefully someone else can help - when using the express router, I have it working just fine with multer to process file uploads. – Mark Hughes May 28 '15 at 11:40
  • Thanks a lot... I'll keep finding and When I find core problem, I will make you know about this. thanks Mark. I have to test on simple project. – YoungHoon Kim May 28 '15 at 12:37
0

It is solved. It is possible that both app.use(bodyparser) and app.use(multer) are used simultaneously.

app.use(bodyparser.json()) app.use(multer())

Like this.

And my previous problem is another points. I don't know exactly what make it doing. I just change express version from 3.x to 4.x and test various situation. In this progress, I find out that req has image buffer properly, and can get buffer data.

Thanks Mark and all of you intereted in my question.

  • Great news - glad you've solved it. I've updated my answer to include the bodyparser.json so it covers the final solution to the problem. – Mark Hughes May 29 '15 at 08:38