简体   繁体   English

将图像从html发送到node.js

[英]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: 但是当我将此图像传递给server.js时,我无法访问它并在控制台上获得未定义的类型,这是我的脚本代码:

 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 这是我的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. 如果仍未定义,请尝试在请求中添加dataType prop并对json数据进行字符串化。 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. 谢谢@FewFlyBy。您回答了这个问题,我在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 并在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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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