1

Hi I am capturing frames from video and converting it in Image. But when i pass this image to server.js I am not able to access it and getting its type as undefined on console.This is my script code:

var canvas= document.getElementById("canvs");
var video=document.getElementById("videoElement");
var png= new Image();
var imcanvas = canvas.getContext("2d");
imcanvas.drawImage(video, 0,0 , canvas.width,canvas.height);	
png = canvas.toDataURL("image/png");
var json={"name":png};
jQuery.ajax({
	url: "/demo",
	type: "POST",
	data:json,
	processData: false,
	cache: false,
	success: function(reponse) {
	  if(reponse) {
	    console.log(reponse);
	  } else {
	    console.log('Error');
	  }
	}
});

This is my server.js

var express = require('express');
var cv = require('./public/opencv');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended:true }));
app.use(express.static('public'));
app.get('/index.html', function (req, res) {
	res.sendFile( __dirname + "/" + "index.html" );
});
app.post('/demo', (req,res)=>{
	console.log(typeof(req.body.name));
});
var server = app.listen(8081, function () {
	var host = server.address().address;
	var port = server.address().port;
});

2 Answers 2

2

Thanks @FewFlyBy.You answered it, only one extra statement i added in server.js. I am able do it like this :

         //above codes
        jQuery.ajax({
                url: "/demo",
                type: "post",
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(json),
                success: function (reponse) {
                    if (reponse) {
                        console.log(reponse);
                    } else {
                        console.log('Error');
                    }
                }
                });

and in server.js

    //require statements
    app.use(bodyParser.json({limit: '50mb'}));
    app.use(bodyParser.urlencoded({
       limit: '50mb',
       extended: true
    }));
    app.post('/demo', (req, res) => {
    const base64data=req.body.image;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Congratulations. Keep it up!
1

It's probably the body's limit.

What you can do is to set the body-parser limit like this:

 app.use(bodyParser.urlencoded({
    limit: '50mb',
    extended: true
 }));

If it's still undefined, try to add dataType prop in your request and stringify the json data. Example:

var json={"name":png};

jQuery.ajax({
    url: "/demo",
    dataType  : 'json',
    type: "POST",
    data: JSON.stringify(json),
    processData: false,
    cache: false,
    success: function(reponse) {
        if(reponse) {
            console.log(reponse);
        } else {
            console.log('Error');
        }
    }
})

1 Comment

Thanks for reply but still i am getting undefined. Is there any other way to pass image to node.js?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.