convert binary content to file

0
Hi I am sending the content of a file through a mendix application using a Webservice. Does anybody know how to convert binary content into a new filedocument? thanks HP
asked
2 answers
2

You could try this code, just create a new Java action in your project with one input parameter which is called: 'targetFile' (case sensitive), this should be a System.FileDocument (any object will give you some compile errors)
This action will copy the binary content from the FileDocument and stores this content as a normal file which can be downloaded with the normal file widget.

Keep in mind this won't remove the content from the binary attribute so this value is still stored into the Database when you don't want that you could use code option 2, this copies the binary content from 'sourceFile' into the normal 'targetFile' you could remove the sourceFile after this Java actio to prevent excessive disk usage.

Deploy your project once and past the code below between the two lines: //BEGIN USER CODE and //END USER CODE


Code option 1: (input param 'targetFile')

java.io.File tmpFile = java.io.File.createTempFile("MxTempFile","MxFile");
java.io.FileOutputStream ous = new java.io.FileOutputStream(tmpFile);
this.targetFile.getContents(this.getContext(), ous);

java.io.FileInputStream is = new java.io.FileInputStream(tmpFile);
com.mendix.core.Core.storeFileDocumentContent(this.getContext(), this.targetFile.getMendixObject(), is);
is.close();
tmpFile.delete();

return true;




Code option 2: (input param 'targetFile' and 'sourceFile')

java.io.File tmpFile = java.io.File.createTempFile("MxTempFile","MxFile");
java.io.FileOutputStream ous = new java.io.FileOutputStream(tmpFile);
this.sourceFile.getContents(this.getContext(), ous);

java.io.FileInputStream is = new java.io.FileInputStream(tmpFile);
com.mendix.core.Core.storeFileDocumentContent(this.getContext(), this.targetFile.getMendixObject(), is);
is.close();
tmpFile.delete();

return true;
answered
1

Yes, you probably need a java-action which encodes the binary file to a Base64EncodedString. According to this post, you can use a java-actions from the community commons module.

answered