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
No properties
Don't instantiate directly instances of the SqlQueryServiceclass.
SqlQueryService instances are automatically injected by Dynamics Mobile framework.
1
import { View, SqlQueryService } from '$dms';
2
import { Customer } from '$dms-bo';
3
4
@View()
5
export class MyView {
6
7
constructor(
8
//we need to only declare the member
9
private SqlQueryService: SqlQueryService
10
){
11
12
}
13
14
async load(){
15
//we can now use the injected instance
16
let value = await this.SqlQueryService.execute(`select * from ${Customer.boTableName}}`);
17
}
18
19
}
Copied!
📳
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
fileWrite method creates new file with given filename and filecontent. The method will overwrite the file if it already exists without throwing error
    fileName: string - file name including directory and extension. myfolder/myfile.json
    fileContent: string - string content encoded in utf-8
void
1
import { FileService } from '@dms';
2
import { Customer } from '@dms-bo';
3
4
...
5
const customers = await Customer.query().executeSelect();
6
await this.FileService.fileWrite('customers/list.json', JSON.stringify(customers));
Copied!

fileRead(fileName: string): Promise<string>

Description
Parameters
Returns
Example
fileRead method reads and returns the content of existing file by given filename. The method throws error if the file does not exists.
    fileName: string - the name of the file including the folder and file extension - myfolder/myfile.json
string content read from the file encoded in utf-8
1
import { FileService } from '@dms';
2
import { Customer } from '@dms-bo';
3
4
...
5
const content = await this.FileService.fileRead('customers/list.json');
6
const customers = JSON.parse(content);
Copied!

fileDelete(fileName: string): Promise<void>

Description
Parameters
Returns
Example
fileDelete method deletes a file by given filename. The method will not throw error if the file does not exists.
    fileName: string - the name of the file including the folder and file extension - myfolder/myfile.json
void
1
import { FileService } from '@dms';
2
import { Customer } from '@dms-bo';
3
4
...
5
await this.FileService.fileDelete('customers/list.json');
Copied!

fileExists(fileName: string): Promise<boolean>

Description
Parameters
Returns
Example
fileExists method returns true if the file with the given filename exists.
    fileName: string - the name of the file including the folder and file extension - myfolder/myfile.json
true if the file exists false if the file does not exists
1
import { FileService } from '@dms';
2
import { Customer } from '@dms-bo';
3
4
...
5
const exists = await this.FileService.fileExists('customers/list.json');
6
if(exists){
7
const content = await this.FileService.fileRead('customers/list.json');
8
}
Copied!

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

Description
Parameters
Returns
Example
getDirFiles method returns list of files contained in the given folder.
    folder: string - the full name of the file folder- myfolder/subfolder
List of strings, where each array element is a full file path for a file contained in the given folder.
1
import { FileService } from '@dms';
2
import { Customer } from '@dms-bo';
3
4
...
5
const files = await this.FileService.getDirFiles('customers');
6
let combinedContent = '';
7
for(let i=0;i<files.length;i++){
8
const filePath = files[i];
9
const content = await this.FileService.readFile(filePath);
10
combinedContent+=content;
11
}
Copied!
Last modified 1yr ago