Reputation: 560
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:
Here is the nodejs server code below:
Tried converting the image toBase64String("JPG"), still doesn't work (code below:)
But it seems that my nativescript code isn't working
Upvotes: 0
Views: 569
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