FileHelper
The FileHelper provides APIs to work with files in Magentrix. Using this class, developers can work with files to get more information or extract a file a ZIP file.
Supported Methods:
| Name | Parameters: | Output |
|---|
| GetFileName | string fileName | string |
| GetFileNameWithoutExtension | string fileName | string |
| IsFileNameValid | string fileName
bool mustHaveExtension = false | bool |
SanitizeFileName | string fileName
bool removeWhiteSpaces = false | string |
ExtractFileFromZip | string zipFileId
string filePath
System.Text.Encoding encoding = null | byte[] |
| CreateZip | List<ZipFileData> files | byte[] |
Below sample shows how you can sanitize file names, this API removes dots and special characters with "Dashes".
If remove white spaces flag is also enabled, the white spaces will also be replaced with "Dashes".
//Where 'fileName' is "My document%12.pdf"string cleansedFileName = FileHelper.SanitizeFileName(fileName, removeWhiteSpaces:true);
//the above code results in: "My-document-12.pdf"
Below code sample shows how you can extract a file from a ZIP file stored in Magentrix document library or an attachment record:
var zipFileId = "<ZIP doc Id>";
byte[] fileBytes = FileHelper.ExtractFileFromZip(zipFileId, "Archives/Docs/my-file-2.docx");
Note that the encoding of the file by default is set to "UTF-8", if you need to open the ZIP with a different encoding, you can pass it as an optional parameter.
Also note that the path is relative to the structure of the ZIP file and you need use "/" character to outline the folder path for the file you want to extract. Please avoid performing this operation for very large ZIP files as you may experience performance issues.
Creating a Zip File
Using the below API you can create new Zip files within Magentrix Platform
var files = new List<ZipFileData>();
//add your files that you want to Zip together:
files.Add(new ZipFileData { FileName = "mySample.pdf", FileBytes: mySamplePdfBytes });
var zipFileBytes = FileHelper.CreateZip(files);