VMware Cloud Community
ca_fahem
Enthusiast
Enthusiast

upload File item in service broker : how to use it

Hello Everyone, 

I have a use case where i want to add upload file item in custom form of a blueprint in service broker, to let the user attach a file that can be read by an approver to know the detail of the provisioning. how i can get the content of the file and decode it and attach it a PDF or a any format possible. any ideas ?

Thank You.

0 Kudos
3 Replies
StefanSchnell
Enthusiast
Enthusiast

@ca_fahem 

Hello Arar,
you can find an approach to handle this with an input form of a workflow in the following post:

https://communities.vmware.com/t5/VMware-Aria-Automation/FileUpload-Parameter-in-vRO-8-1/td-p/228734...

Here a guide to add these input forms to the service broker catalog:

https://docs.vmware.com/en/vRealize-Automation/8.11/Using-and-Managing-Service-Broker/GUID-416D6130-...

Best regards
Stefan


More interesting information at blog.stschnell.de

ca_fahem
Enthusiast
Enthusiast

@StefanSchnell @StefanSchnell  Thank you for your answer.

Spoiler

I used an input in my service  broker request (type MimeAttachement) and map another input with vRO action that request MimeAttachement input (binded with my field), so I can no get the content, the name and the type but in base64 coding. But i couldn't decode the content using any function to convert a string base64 to normal format, or save the file (so later i will show it or send it to the approvers). i had many questions: like the function Atob() or Bulb( does not exist in vRO) i didn't find PDFReader, Atob, Buffer...

  // Check if the attachment has content
  if (Attachement ) {
    // Decode the Base64-encoded content
    const decodedContent = Base64.decode(Attachement.content);
// Create a data URL for the PDF content
 
//use PdfFileReader

  // Return the data URL as a link to download the PDF
  return dataURL;
} else {
  System.log('Attachment does not have content.');
  // Return an error message or handle the case when there is no content.
}
 
or 
 

function base64ToPDF(base64String, fileName) {
// Decode the Base64 string to a binary ArrayBuffer
const binaryString = atob(base64String);
const arrayBuffer = new ArrayBuffer(binaryString.length);
const uint8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < binaryString.length; i++) {
uint8Array[i] = binaryString.charCodeAt(i);
}

// Create a Blob object from the ArrayBuffer
const blob = new Blob([arrayBuffer], { type: 'application/pdf' });

// Create a URL for the Blob object
const url = URL.createObjectURL(blob);

// Create an 'a' element to trigger the download
const a = document.createElement('a');
a.href = url;
a.download = fileName || 'document.pdf';

 

StefanSchnell
Enthusiast
Enthusiast

@ca_fahem 

Hello Arar,

here an example to store the file in the temporary directory:

1. Define in Inputs/Outputs an input variable, in our case in_file.

StefanSchnell_0-1697178478004.png

2. Add in the workflow a scriptable task and assign as input to this in_file. In this case I didn't rename the variable.

StefanSchnell_1-1697178607796.png

3. Following lines of code as an example to save any file in the temporary directory.

StefanSchnell_2-1697178856797.png

The function saveFileContent saves, with the method write, from the mime parameter, the file in the temporary directory. The lines above checks if a file with the same name exists and if the file exists it will be deleted.

The function fileExists checks if a file with a specific name exists in the temporary directory and if it exists a message is displayed.

// Begin----------------------------------------------------------------

function saveFileContent(mime) {
  var file = new File(System.getTempDirectory() + "/" + mime.name);
  if (file.exists) {
    file.deleteFile();
  }
  mime.write(System.getTempDirectory(), null);
}

function fileExists(fileName) {
  var file = new File(System.getTempDirectory() + "/" + fileName);
  if (file.exists) {
    System.log("File " + fileName + " exists");
  }  
}

saveFileContent(in_file);
fileExists(in_file.name);

// End------------------------------------------------------------------

4. Result

StefanSchnell_3-1697179258752.png

StefanSchnell_4-1697179295978.png

Any file should be able to be saved this way.

Best regards
Stefan

 

 

 

 

 

 


More interesting information at blog.stschnell.de

0 Kudos