1

I got base64 object from canvas

var imageData = canvas.toDataURL('image/png');

And I try to send this base64 object (imageData) to server via ajax:

var _data = {};
// set test property:
_data.avatar = imageData;
console.log(_data);
// Make an Ajax request
$.post('./setup', _data, function(result) {
console.log('result', result);
});  

In my nodejs server (using only express midleware), I got a simple router to process this request

router.post('/setup', function(req, res, next) {
    console.log('setup: data', req.body.data);
    console.log('setup: check', req.body.check);
    console.log('setup avatar: ', req.body.avatar);
    res.json({
    success:true
    });
});

Actually, it is not working and I wonder why the base64 object cannot be sent. In another cases my json object with text inside can send to server nomally.. Thank for your help and solution to send base64 object to expressjs server

hoanganh17b
  • 727
  • 1
  • 10
  • 25
  • What are the outputs of the console.logs? Side note: Are you using `bodyParser`? – Swaraj Giri Jul 23 '15 at 11:27
  • Yes...I am using bodyParse @Swaraj Giri...nothing in console log in my server – hoanganh17b Jul 23 '15 at 11:38
  • Check `req.body` and check what the ajax request is sending in the developer console. Are you serializing the data that you are sending via ajax? – Swaraj Giri Jul 23 '15 at 11:40
  • noting inside the req.body...I was doing a test with serializing and non-serializing data but no case can pass my base64 obj to server. ??? – hoanganh17b Jul 23 '15 at 11:43
  • Actually, the command like of console is not triggerd.. @Swaraj Giri.. Just only morgan output tell me that the post requets is comming on server...:( – hoanganh17b Jul 23 '15 at 11:44
  • Serialize your data, check if the xhr request is getting triggered, check the `XHR` tab in dev tools in browser. – Swaraj Giri Jul 23 '15 at 11:46
  • Remote Address:127.0.0.1:50315 Request URL:http://localhost:3000/api/setup Request Method:POST Status Code:413 Request Entity Too Large – hoanganh17b Jul 23 '15 at 11:51

2 Answers2

5

Try to increase the limit of data your body parser can handle like:

app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
mynawaz
  • 1,589
  • 1
  • 7
  • 16
1

Have a look at this answer on how to set the allowed body size:

Community
  • 1
  • 1
Tobi
  • 30,855
  • 7
  • 52
  • 89