1# 数据标签 2 3该模块提供文件数据安全等级的相关功能:向应用程序提供查询、设置文件数据安全等级的JS接口。 4 5>  **说明:** 6> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7 8## 导入模块 9 10```js 11import securityLabel from '@ohos.securityLabel'; 12``` 13 14## 使用说明 15 16使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考: 17 18```js 19import featureAbility from '@ohos.ability.featureAbility'; 20let context = featureAbility.getContext(); 21let path = ''; 22context.getFilesDir().then((data) => { 23 path = data; 24}) 25``` 26 27## securityLabel.setSecurityLabel 28 29setSecurityLabel(path:string, type:dataLevel):Promise<void> 30 31以异步方法设置数据标签,以promise形式返回结果。 32 33**系统能力**:SystemCapability.FileManagement.File.FileIO 34 35**参数:** 36 37| 参数名 | 类型 | 必填 | 说明 | 38| --------- | ------ | ---- | -------------------------------------------- | 39| path | string | 是 | 文件路径 | 40| type | dataLevel | 是 | 文件等级属性,只支持"s0","s1","s2","s3","s4" | 41 42**返回值:** 43 44 | 类型 | 说明 | 45 | ------------------- | ---------------- | 46 | Promise<void> | Promise实例,用于异步获取结果。本调用将返回空值。| 47 48**示例:** 49 50 ```js 51 let type = "s4"; 52 securityLabel.setSecurityLabel(path, type).then(function(){ 53 console.info("setSecurityLabel successfully"); 54 }).catch(function(error){ 55 console.info("setSecurityLabel failed with error:" + error); 56 }); 57 ``` 58 59## securityLabel.setSecurityLabel 60 61setSecurityLabel(path:string, type:dataLevel, callback: AsyncCallback<void>):void 62 63以异步方法设置数据标签,以callback形式返回结果。 64 65**系统能力**:SystemCapability.FileManagement.File.FileIO 66 67**参数:** 68 69| 参数名 | 类型 | 必填 | 说明 | 70| --------- | ------------------------- | ---- | -------------------------------------------- | 71| path | string | 是 | 文件路径 | 72| type | dataLevel | 是 | 文件等级属性,只支持"s0","s1","s2","s3","s4" | 73| callback | AsyncCallback<void> | 是 | 是否设置数据标签之后的回调 | 74 75**示例:** 76 77 ```js 78 let type = "s4"; 79 securityLabel.setSecurityLabel(path, type, function(error){ 80 console.info("setSecurityLabel:" + JSON.stringify(error)); 81 }); 82 ``` 83## securityLabel.setSecurityLabelSync 84 85setSecurityLabelSync(path:string, type:dataLevel):void 86 87以同步方法设置数据标签。 88 89**系统能力**:SystemCapability.FileManagement.File.FileIO 90 91**参数:** 92 93| 参数名 | 类型 | 必填 | 说明 | 94| --------- | ------ | ---- | -------------------------------------------- | 95| path | string | 是 | 文件路径 | 96| type | dataLevel | 是 | 文件等级属性,只支持"s0","s1","s2","s3","s4" | 97 98**示例:** 99 100```js 101let type = "s4"; 102securityLabel.setSecurityLabelSync(path, type); 103``` 104 105## securityLabel.getSecurityLabel 106 107getSecurityLabel(path:string):Promise<string> 108 109异步方法获取数据标签,以promise形式返回结果。 110 111**系统能力**:SystemCapability.FileManagement.File.FileIO 112 113**参数:** 114 115 | 参数名 | 类型 | 必填 | 说明 | 116 | ------ | ------ | ---- | -------- | 117 | path | string | 是 | 文件路径 | 118 119**返回值:** 120 121 | 类型 | 说明 | 122 | --------------------- | ------------ | 123 | Promise<string> | 返回数据标签 | 124 125**示例:** 126 127 ```js 128 let type = "s4"; 129 securityLabel.getSecurityLabel(path).then(function(type){ 130 console.log("getSecurityLabel successfully:" + type); 131 }).catch(function(error){ 132 console.log("getSecurityLabel failed with error:" + error); 133 }); 134 ``` 135 136## securityLabel.getSecurityLabel 137 138getSecurityLabel(path:string, callback:AsyncCallback<string>): void 139 140异步方法获取数据标签,以callback形式返回结果。 141 142**系统能力**:SystemCapability.FileManagement.File.FileIO 143 144**参数:** 145 146 | 参数名 | 类型 | 必填 | 说明 | 147 | -------- | --------------------------- | ---- | -------------------------- | 148 | path | string | 是 | 文件路径 | 149 | callback | AsyncCallback<string> | 是 | 异步获取数据标签之后的回调 | 150 151**示例:** 152 153 ```js 154 let type = "s4"; 155 securityLabel.getSecurityLabel(path,function(error, type){ 156 console.log("getSecurityLabel successfully:" + type); 157 }); 158 ``` 159## securityLabel.getSecurityLabelSync 160 161getSecurityLabelSync(path:string):string 162 163以同步方法获取数据标签。 164 165**系统能力**:SystemCapability.FileManagement.File.FileIO 166 167**参数:** 168 169| 参数名 | 类型 | 必填 | 说明 | 170| ------ | ------ | ---- | -------- | 171| path | string | 是 | 文件路径 | 172 173**返回值:** 174 175| 类型 | 说明 | 176| ------ | ------------ | 177| string | 返回数据标签 | 178 179**示例:** 180 181```js 182let result = securityLabel.getSecurityLabelSync(path); 183console.log("getSecurityLabel successfully:" + result); 184``` 185