You can use SaveToFile in an Executable Action Language (EAL) action to write text or binary data to a file on the machine that executes the ViewModel logic.
Syntax
selfVM.SaveToFile(filename, data)
| Argument | Type | Description |
|---|---|---|
filename
|
String | The complete destination path and file name. Include the file extension that identifies the content, such as .csv, .xml, .json, .png, or .pdf.
|
data
|
String or Blob | The content to write. Use a String for text content and a Blob for binary content such as an image or PDF. |
Save a file from an EAL action
- Create or open the action that produces the content to export.
- Build a complete destination path in a String. Use forward slashes (
/) in the path. - Pass the path and the String or Blob value to
selfVM.SaveToFile. - Run the action in the target environment and verify that the executing machine can write to the destination folder.
For example, this action writes the selected customer's profile picture to a PNG file:
selfVM.SaveToFile('C:/Exports/' + vCurrent_Customer.Name + '.png', vCurrent_Customer.ProfilePicture)
In this example:
'C:/Exports/' + vCurrent_Customer.Name + '.png'creates the destination file name.vCurrent_Customer.ProfilePictureis the binary Blob that is written to that file.
Save text content
Use a String when you have generated text in a format such as CSV, JSON, or XML. The file extension must match the content you intend to create.
selfVM.SaveToFile('C:/Exports/customer.xml', selfVM.ViewModelAsXml)
This example writes the ViewModel's XML representation to C:/Exports/customer.xml. Configure the XML representation as described in Documentation:OCLOperators ViewModelAsXml.
Where the file is written
SaveToFile writes to the disk of the machine that executes the logic.
| Execution context | File destination |
|---|---|
| MDriven Turnkey web application | The MDrivenServer disk. The file is not downloaded to the web user's local Downloads folder. |
| Other execution context | The disk available to the machine and process that execute the ViewModel logic. |
If your requirement is to give a web user a downloaded file, do not treat a server-side path as a browser download path. Design the surrounding flow for the required delivery mechanism.
Paths, permissions, and overwriting
Use forward slashes in EAL paths
Do not end a path String with a backslash. For example, 'C:\Temp\' causes a syntax error because the OCL parser interprets the final backslash as escaping the closing quote.
Use forward slashes instead:
'C:/Temp/export.csv'
Grant write access
The account running the MDrivenServer service must have write permission for the destination folder. For a Turnkey deployment, verify this permission on the server before running the action.
Existing files are replaced
SaveToFile overwrites an existing file with the same path and name. It does not ask for confirmation. Include a unique value in the file name when you must preserve earlier exports.
Related persistence behavior
Writing a file does not replace saving changes made in the ViewModel. Use Save when your action must commit ViewModel changes to persistent storage.
