Image to binary

Hello.

It´s possible convert a image to binary in Servoy?

I need send data to a web service and the image must be in binary.

Thanks

Servoy 7.4.3

Not sure what exactly you mean when you say “image”, but suppose you have a JSImage (plugins.images.JSImage) then you could use jsImage.getData() to get a byte array.

But I think that when you want to send bytes with the http plugin, you need to use a (temp) file and use putOrPostRequest.addFile(…).

Hi Juan.

That’s the way I manage images. In this case I create a thumbnail for better perfomance in the form preview.

function subirFotoServidor(jsfile, record){
	
	plugins.file.streamFilesToServer(jsfile.getAbsolutePath(), "/fotos/" + record.md5);
	var image = plugins.images.getImage(jsfile.getAbsolutePath());
	image = image.resize(500,500);
	var bytes = image.getData();
	var tempFile = plugins.file.createTempFile("","");
	plugins.file.writeFile(tempFile, bytes);
	plugins.file.streamFilesToServer(tempFile, "/fotos/" + record.md5 + "." + "500x500");
	plugins.file.deleteFile(tempFile);
	
	return 1;
}

Let me know if this is the case you are talking about.

Hello

I need upload a image (d:/photo/image.jpg) to webservice

This is the code:

 var image = plugins.images.getImage('d:/photo/image.jpg');
	    var bytes = image.getData();
	   var tempFile = plugins.file.createTempFile("","");
	   plugins.file.writeFile(tempFile, bytes);

var url='https://'+xxxxxxxxxxxx+'/api/images/products/10?';
var cliHttp = plugins.http.createNewHttpClient();
var postRequest = cliHttp.createPostRequest(url);
postRequest.addFile(null,tempFile);
response=postRequest.executeRequest();

The value of ```
postRequest.addFile(null,tempFile);


<?xml version="1.0" encoding="UTF-8"?>

xmlns:xlink=“http://www.w3.org/1999/xlink”

```

The only requeriments for the webservice are post method and binary value of the image.

Thanks

With that html form, I can send the image correctly.

<html>
<body>
	<form enctype="multipart/form-data" method="POST" action=
	"https://xxxxxxxxxxxxxxxx/api/images/products/10">
  <fieldset>
    <legend>Add image for products No 1</legend>
    <input type="file" name="image">
    <input type="submit" value="Execute">
  </fieldset>
</form>
</body>
</html>

What would be the equivalent option in Servoy?

Thanks

Finally I found the solution.

This is the code:

 var image = plugins.images.getImage(_ar.foto);
	   var bytes = image.getData();
	   var content=image.getContentType()
	    var tempFile = plugins.file.createTempFile("",".jpg");
var postRequest = cliHttp.createPostRequest(url);
	postRequest.addHeader('enctype','multipart/form-data');
	postRequest.addHeader('method','POST');
	postRequest.addParameter('Content-Type',content);
	postRequest.addFile('image',tempFile);
	response=postRequest.executeRequest();

Thanks