UiPath Studio - How to Read a File, Convert it to Base64String, Convert Base64String to Bytes, Write it to a New File, and Retrieve its System.IO.FileInfo?
In UiPath Studio, understanding how to read a file, convert it to a Base64 string, decode that string back to bytes, write it to a new file, and retrieve its System.IO.FileInfo can be invaluable for several reasons:
- Data Security and Privacy: Base64 encoding is often used to obfuscate data and prevent it from being easily read. Therefore, understanding this process is beneficial in scenarios where data privacy and security are paramount.
- Binary Data Handling: Some data formats, such as images, documents, or other file types, are binary. To process and use them in text-based formats (like JSON, XML), they need to be Base64 encoded. Knowing how to convert data to and from Base64 enables users to work with a variety of data types without loss or distortion.
- Interacting with APIs: A lot of APIs require the uploading of binary data as Base64 encoded strings. This is common when dealing with image uploads, file attachments, etc. By learning this process, developers can effectively interact with such APIs.
- File Management: Being able to create files, retrieve, and use file information (like the path, name, size, and creation date) is a fundamental part of automating file-based processes.
By understanding and mastering these techniques in UiPath Studio, users can develop efficient, effective, and secure automation workflows to handle files and data.
Resolution
This conversion can be done in UiPath Studio with VB.NET language support with the help of UiPath.System.Activities package and Invoke Code activity:
Assume the existence of an Excel File "C:\Users\username\Documents\UiPath\ConvertStringAndFileToBase64\07-14-2024-Report.xlsx" with the below content which needs to be converted as Base64String, then converted back to bytes, write it to a new file, and retrieve its System.IO.FileInfo.
Add an Invoke Code activity for a VB.Net Language with the below code syntax (Feel free to adapt the code based on the needs):
Dim bytes As Byte() Dim base64File As String Dim fileBytes As Byte() Dim inputFilePath As String = "C:\Users\username\Documents\UiPath\ConvertStringAndFileToBase64\07-14-2024-Report.xlsx" Dim outputFilePath As String = "C:\Users\username\Documents\UiPath\ConvertStringAndFileToBase64\07-14-2024-Report_Decoded.xlsx"Try
’ Read all bytes from a file into a byte array
bytes = File.ReadAllBytes(inputFilePath)
Console.WriteLine(“File " & inputFilePath & " successfully read.”)
Catch ex As FileNotFoundException
Console.WriteLine("File not found error: " & ex.Message)
Catch ex As Exception
Console.WriteLine("General error: " & ex.Message)
End TryTry
’ Convert the strCurrentFile as base64 string
base64File = Convert.ToBase64String(bytes)
Console.WriteLine(“File " & inputFilePath & " successfully converted to Base64.”)
Catch ex As Exception
Console.WriteLine("Converting to Base64 error: " & ex.Message)
End TryTry
’ Convert base64 string to byte
fileBytes = Convert.FromBase64String(base64File)
Console.WriteLine(“Base64 string successfully converted to byte array.”)
Catch ex As Exception
Console.WriteLine("Converting from Base64 string to byte array error: " & ex.Message)
End TryTry
’ Choose a location and filename for your file (this could be dynamic)
File.WriteAllBytes(outputFilePath, fileBytes)
Console.WriteLine("Bytes successfully written to new file at " & outputFilePath)
Catch ex As Exception
Console.WriteLine("Writing bytes to file error: " & ex.Message)
End TryTry
’ Get FileInfo
Dim fileInfo As FileInfo = New FileInfo(outputFilePath)
Console.WriteLine(“FileInfo for " & outputFilePath & " retrieved successfully.”)
Catch ex As Exception
Console.WriteLine("Retrieving FileInfo error: " & ex.Message)
End Try’ Check if the new file was created
If File.Exists(outputFilePath) Then
Console.WriteLine(“New file at " & outputFilePath & " was created successfully.”)
Else
Console.WriteLine(“New file was NOT created.”)
End If
After the Invoke Code activity is executed, all the operations specified in the code will be carried out, resulting in the creation of a new file with the original content intact.