简体   繁体   中英

Send image from html to node.js

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; }); 

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');
        }
    }
})

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM