FileService
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.
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}}`);
}
}
📳
🌐
There is a difference in the behaviour of the FileService API in mobile and backend apps. Read the File Access documentation for more details
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
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));
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
import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
const content = await this.FileService.fileRead('customers/list.json');
const customers = JSON.parse(content);
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
import { FileService } from '@dms';
import { Customer } from '@dms-bo';
...
await this.FileService.fileDelete('customers/list.json');
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
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');
}
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.
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;
}
Last modified 3yr ago