0

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

Miguel Lorenzo
  • 560
  • 1
  • 5
  • 12
  • Consider using code snippets instead of screenshots to ease code reproduction. Where base64 and utf8 comes from (not part of the NativeScript APi) Also you are reading text from an image file (not sure if that works as expected). You can use your image soure with toBase64String to generate base64 https://docs.nativescript.org/api-reference/classes/_image_source_.imagesource.html#tobase64string – Nick Iliev Jul 06 '16 at 09:55
  • @NickIliev tried converting my image to base64 it still doesn't work, the server still receives nothing :( – Miguel Lorenzo Jul 06 '16 at 11:14
  • try testing on a known dummy server like httpbin or other known service. Also try to log the server error or response – Nick Iliev Jul 06 '16 at 11:28
  • @NickIliev made my own server, my nativescript code seems to work, tried to upload an image on the nodejs server above also seems to work. But when i try to upload an image coming from the nativescript app to the nodejs server, still doesn't work :( – Miguel Lorenzo Jul 06 '16 at 12:29
  • Are you deploying in iOS!? If yes - Keep in mind that http is not allowed transfer protocol - you should either use https or "hack" the info.plist (do it only for for testing purposes) http://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9 – Nick Iliev Jul 06 '16 at 12:34
  • @NickIliev im deploying my work on a android emulator – Miguel Lorenzo Jul 06 '16 at 19:08
  • I have revised my answer with extended code to see how to parse your response (based on httpbin but the principle should be the same) – Nick Iliev Jul 07 '16 at 08:45

1 Answers1

2

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);
    });
})
Nick Iliev
  • 9,610
  • 3
  • 35
  • 89