FileService

Overview

SERVICE This class is a "service" and it is automatically injected if declared as a member in the constructor of the consuming class. - See the constructor section below for details - See the Automatic Instance Injection page for details on the topic

The FileService class provides methods to work with files

Properties
Methods
Constructor
App Types
Properties

No properties

Methods
Constructor

Don't instantiate directly instances of the SqlQueryServiceclass.

SqlQueryService instances are automatically injected by Dynamics Mobile framework.

import { View, SqlQueryService } from '$dms';
import { Customer } from '$dms-bo';
@View()
export class MyView {
constructor(
//we need to only declare the member
private SqlQueryService: SqlQueryService
){
}
async load(){
//we can now use the injected instance
let value = await this.SqlQueryService.execute(`select * from ${Customer.boTableName}}`);
}
}
App Types

📳 Mobile 🌐 Backend

There is a difference in the behaviour of the FileService API in mobile and backend apps. Read the File Access documentation for more details

fileWrite(fileName: string, fileContent:string): Promise<void>

Description
Parameters
Returns
Example
Description

fileWrite method creates new file with given filename and filecontent. The method will overwrite the file if it already exists without throwing error

Parameters
  • fileName: string - file name including directory and extension. myfolder/myfile.json

  • fileContent: string - string content encoded in utf-8

Returns

void

Example

import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
const customers = await Customer.query().executeSelect();
await this.FileService.fileWrite('customers/list.json', JSON.stringify(customers));

fileRead(fileName: string): Promise<string>

Description
Parameters
Returns
Example
Description

fileRead method reads and returns the content of existing file by given filename. The method throws error if the file does not exists.

Parameters
  • fileName: string - the name of the file including the folder and file extension - myfolder/myfile.json

Returns

string content read from the file encoded in utf-8

Example
import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
const content = await this.FileService.fileRead('customers/list.json');
const customers = JSON.parse(content);

fileDelete(fileName: string): Promise<void>

Description
Parameters
Returns
Example
Description

fileDelete method deletes a file by given filename. The method will not throw error if the file does not exists.

Parameters
  • fileName: string - the name of the file including the folder and file extension - myfolder/myfile.json

Returns

void

Example
import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
await this.FileService.fileDelete('customers/list.json');

fileExists(fileName: string): Promise<boolean>

Description
Parameters
Returns
Example
Description

fileExists method returns true if the file with the given filename exists.

Parameters
  • fileName: string - the name of the file including the folder and file extension - myfolder/myfile.json

Returns

true if the file exists false if the file does not exists

Example
import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
const exists = await this.FileService.fileExists('customers/list.json');
if(exists){
const content = await this.FileService.fileRead('customers/list.json');
}

getDirFiles(folder: string): Promise<Array<string>>

Description
Parameters
Returns
Example
Description

getDirFiles method returns list of files contained in the given folder.

Parameters
  • folder: string - the full name of the file folder- myfolder/subfolder

Returns

List of strings, where each array element is a full file path for a file contained in the given folder.

Example
import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
const files = await this.FileService.getDirFiles('customers');
let combinedContent = '';
for(let i=0;i<files.length;i++){
const filePath = files[i];
const content = await this.FileService.readFile(filePath);
combinedContent+=content;
}