Miguel Lorenzo
Miguel Lorenzo

Reputation: 560

Nativescript uploading an image on a nodejs server that uses formidable to handle inputs

Hi I was wondering if anyone can help me upload an image taken by a camera module from nativescript through a nodejs server that uses formidable js to handle inputs

Here is my nativescript code below: enter image description here

Here is the nodejs server code below: enter image description here

Tried converting the image toBase64String("JPG"), still doesn't work (code below:) enter image description here

nodejs server output enter image description here

But it seems that my nativescript code isn't working

Upvotes: 0

Views: 569

Answers (1)

Nick Iliev
Nick Iliev

Reputation: 9670

Here is a very simple way to convert an image source from the cameraModule to base64String and from the to pass it to your server

 cameraModule.takePicture().then(function(imageSource) {
    var imageAsBase64String = imageSource.toBase64String("JPG");
    return imageAsBase64String; 
}).then(function (imageAsBase64String) {
    http.request({
        url: "https://httpbin.org/post",
        method: "POST",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({ name: "myName", imageAsString: imageAsBase64String })  
    }).then(function (response) {
        var result = response.content.toJSON();

        console.log("args: " + result.args);
        console.log("origin: " + result.origin);
        console.log("headers: " + result.headers);
        console.log("json: " + result.json);
        console.log("url: " + result.url);
        // console.log("data: " + result.data); // this is our send content

        var myObj = JSON.parse(result.data); // as we are passing a stringied JSON we ahve to parse it
        console.log("my Image Name: " + myObj.name);
        console.log("my Image Base64String: " + myObj.imageAsString);

    }, function (e) {
        console.log("Error occurred " + e);
    });
})

Upvotes: 2

Related Questions