Image to binary

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Image to binary

Postby JuanMartin » Thu Feb 23, 2017 1:32 pm

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
Juan Antonio Martín Pinilla
Servoy 2022.9.2 - PostgreSql 9.4
software@critenerife.com
User avatar
JuanMartin
 
Posts: 169
Joined: Tue Nov 25, 2008 9:20 pm
Location: Tenerife - Spain

Re: Image to binary

Postby patrick » Thu Feb 23, 2017 1:55 pm

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(...).
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: Image to binary

Postby Roberto Blasco » Tue Feb 28, 2017 11:44 am

Hi Juan.

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

Code: Select all
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.
Un saludo. Roberto.

Madrid - Spain
Tfno: (+34) 625653066
E-mail: roberto.blasco.serrano@gmail.com
User avatar
Roberto Blasco
007
 
Posts: 355
Joined: Tue Apr 08, 2008 7:18 pm
Location: Madrid / Spain

Re: Image to binary

Postby JuanMartin » Wed Mar 01, 2017 10:28 pm

Hello

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

This is the code:

Code: Select all
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
Code: Select all
postRequest.addFile(null,tempFile);
it´s true, but the response of the webservice is:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code>
<![CDATA[66]]>
</code>
<message>
<![CDATA[Unable to save this image]]>
</message>
</error>
</errors>



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

Thanks
Juan Antonio Martín Pinilla
Servoy 2022.9.2 - PostgreSql 9.4
software@critenerife.com
User avatar
JuanMartin
 
Posts: 169
Joined: Tue Nov 25, 2008 9:20 pm
Location: Tenerife - Spain

Re: Image to binary

Postby JuanMartin » Mon Mar 06, 2017 10:31 am

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

Code: Select all
<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
Juan Antonio Martín Pinilla
Servoy 2022.9.2 - PostgreSql 9.4
software@critenerife.com
User avatar
JuanMartin
 
Posts: 169
Joined: Tue Nov 25, 2008 9:20 pm
Location: Tenerife - Spain

Re: Image to binary

Postby JuanMartin » Sat Mar 25, 2017 1:02 pm

Finally I found the solution.

This is the code:

Code: Select all
var image = plugins.images.getImage(_ar.foto);
      var bytes = image.getData();
      var content=image.getContentType()
       var tempFile = plugins.file.createTempFile("",".jpg");


Code: Select all
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
Juan Antonio Martín Pinilla
Servoy 2022.9.2 - PostgreSql 9.4
software@critenerife.com
User avatar
JuanMartin
 
Posts: 169
Joined: Tue Nov 25, 2008 9:20 pm
Location: Tenerife - Spain


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 18 guests