Call a Microflow from javascript with either string as parameter or image data as image/JPG;base64,string

0
Is it possible to directly pass a string parameter value from javascript to a Microflow? If yes, can you please guide. I tried as below but didn't work. The microflow is triggered, but the parameter object keeps being empty, I tried to save and commit as suggested  but didn’t worked. can anyone help? Tried Method 1: Method 1: function passStringToMicroflow(stringValue) { mx.data.create({ entity: "Signature.ImageNP", callback: function (obj) { obj.set('ImageNPname', stringValue); object.set('ImageNPname', stringValue); mx.data.action({ params: { applyto: "selection", actionname: "Signature.signMF", guids: [obj.getGuid()], }, }); } obj.save(); mx.data.commit(obj); }); }   Tried Method 2: by this even microflow is not triggered.  Method 2 function passStringToMicroflow(stringValue) { mx.data.create({ entity: "Signature.ImageNP", callback: function (object) { object.set('ImageNPname', stringValue); mx.processor.xasAction({ error: function () { logger.error(this.id + "error: error while executing search"); }, actionname: "Signature.signMF", guids: [object.getGUID()] }); } object.save(); mx.data.commit(object); }); } Can someone point me in the direction of what is lacking or wrong in the code above? or please suggest any other right way to do. Note: In 'stringValue' i have 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t+KAAAgAElEQVR4Xu2dXaytWVpW5.........' data. I need to save in entity so that later i can retrive the image. If any other way please suggest.   
asked
3 answers
2

I do not understand exactly why you are doing it like this. Do you want to store the base 64 encoded string as a FileDocument / Image? If so, you should do it not with an input string for a microflow, but with:

mx.data.saveDocument(this.fileGuid, fileName + ".jpg", { width: 180, height: 180 }, fileBlob, lang.hitch(this,function() {
	// success: if file document is saved succesfully, also call additional microflow if configured
	if (this.microflow){
		this._execMicroflow();
	}
}), lang.hitch(this,function(e) {
	mx.ui.info(this.id + ": Error occurred attempting to save document: " + e);
}));

The base64 encoded image you can convert to an image with JS with:

var fileBlob = this._b64toBlob(dataUrl.replace("data:image/png;base64,", ""), "image/jpeg");

_b64toBlob : function(b64Data, contentType, sliceSize) {
	
  contentType = contentType || '';
  sliceSize = sliceSize || 512;

  var byteCharacters = atob(b64Data);
  var byteArrays = [];

  for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
	var slice = byteCharacters.slice(offset, offset + sliceSize);

	var byteNumbers = new Array(slice.length);
	for (var i = 0; i < slice.length; i++) {
	  byteNumbers[i] = slice.charCodeAt(i);
	}

	var byteArray = new Uint8Array(byteNumbers);

	byteArrays.push(byteArray);
  }

  var blob = new Blob(byteArrays, {type: contentType});
  return blob;
}

Does this help?

Also; please state the Mendix version you are using; the Client API has different functions per version, so could very well be that you / me are referring to changed functions...

answered
1

is this the same as:

https://forum.mendix.com/link/questions/88255

 

you may find solution there.

answered
0

Hello Ivo Sturm, I am trying integrating a signature device to mendix application with javascript API, I am calling the javascript by a widget from app store "JavaScriptButton" which is assigning the SRC to IMG inside HTMLSnippetWithContext widget which i got from Appstore. 

The Function from API is as : 

function generateImage() {
    signatureImage = document.getElementById("signatureImage");

    var signatureCanvas = document.createElement("canvas");
    signatureCanvas.id = "signatureCanvas";
    signatureCanvas.height = signatureImage.height;
    signatureCanvas.width = signatureImage.width;
    var signatureCtx = signatureCanvas.getContext("2d");

    clearCanvas(signatureCanvas, signatureCtx);
    signatureCtx.lineWidth = 1;
    signatureCtx.strokeStyle = 'blue';
    lastPoint = { "x": 0, "y": 0 };
    isDown = false;
    for (var i = 0; i < m_penData.length; i++) {
        processPoint(m_penData[i], signatureCanvas, signatureCtx);
    }
    signatureImage.src = signatureCanvas.toDataURL("image/jpeg");

    var ImageCode = signatureCanvas.toDataURL("image/jpeg");
 
    return ImageCode;

}

 

In 'ImageCode' i have string / data as  'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t+KAAAgAElEQVR4Xu2dXaytWVpW5.........' data. I need to save in entity with reference to other field value inside mendix application page. so that later i can retrieve the image.

So, I found some code to return this value to microflow and then from microflow save to entity (as I tried in METHOD 1). But getting empty data in microflow.

I am new to mendix development, Can you please provide the relation between my function generateImage()  above and how I can implement your suggested code? I am using 6.10.2 modeler.

I am waiting for you kind reply.. thank you in advance...

answered