0

I am using Bluemix to develop a 'HTTP POST listener' with NodeJS. This server should be the link between an Android Application and a Watson Bluemix Service

This is my code

/*eslint-env node*/
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');

// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');

// create a new express server
var app = express();

// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));

// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();

/* 'BODY PARSER - NOT WORKING' */
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));

app.use(bodyParser.json());     //Assuming JSON  ENCODED INPUT
app.use(express.bodyParser({uploadDir:'/images'}));

// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {

// print a message when the server starts listening
console.log("server starting on " + appEnv.url);

app.post('/handle',function(request,response){
    var image64=request.body.encoded_String;
    var imageName=request.body.image_name;

    /*OK LOG THIS (Encoded Base64 image)*/
    console.log("IMG RECEIVED: " + imageName); //OK
    console.log("ENCODED: " + image64);        // = undefined (chunk problems?)

    response.writeHead(200, { "Content-Type": "text/plain" });
    response.write('Hello World - Example...\n');
    response.end(); 

    });

});

How can I receive a base64 encoded image and save it to a folder?

Thanks for you help!

Gian0508
  • 79
  • 1
  • 2
  • You did not tell who should receive an image. Nor who is sending it. – greenapps Mar 03 '16 at 13:22
  • `console.log("IMG RECEIVED: " + imageName); //OK` So this code receives the image. And this code is for the .... ? – greenapps Mar 03 '16 at 13:24
  • I should receive by POST an image `app.post('/handle',function(request,response){ var image64=request.body.encoded_String; var imageName=request.body.image_name;` but in image64 I can only read undefined in console log – Gian0508 Mar 03 '16 at 14:40
  • Repeating your code does not make sense. Even if you do it twice. Better give more info. – greenapps Mar 03 '16 at 17:32

1 Answers1

1

String with image received in base64 has usually it's format written at the beginning which has to be removed (or at least I used to remove it).

var base64Data = str.replace(/^data:image\/png;base64,/, ""); // str - string with image

Then you have to save it with fs:

fs.writeFile("../dir/to/save/image.png", base64Data, 'base64', function(err) {});

And that's basically all.

Nonemoticoner
  • 650
  • 5
  • 14
  • Thank you for your help. I have another problem before I'll able to save this image: how can save the string encoded in base64? When I receive a POST i am only able to read `var imageName=request.body.image_name;` and in `image64=request.body.encoded_String;` I can only find undefined... – Gian0508 Mar 03 '16 at 14:53
  • @Gian0508 This is front-end issue. You should read docs depending on what framework you use to send a http request to server. – Nonemoticoner Mar 03 '16 at 16:36