Module: miot/host/file

本地文件访问及处理服务 注意插件文件处理皆处于插件沙盒目录下

Example
//给定文件名后下载或者截图后被放到本地目录里, 在<Image/>等标签需要引用时, 使用{local:"filename"}的方式引入
const myfile = "testpicture.png"
Host.file.downloadFile("http://..../...png", myfile)
.then(res=>{
    const myimg = <Image source={{local:myfile}} ... />
    ...
})
.catch(err=>{...})

...
const myshotfile = "testshot.png"
Host.file.screenShot(myshotfile)
.then(res=>{
   const myshotpic = <Image source={{local:myshotfile}} ... />
   ...
});
...

Methods

(static) amapScreenShot(viewRef, imageName) → {Promise}

高德地图截屏

Parameters:
Name Type Description
viewRef number

MAMapView(MHMapView的父类)的引用

imageName string

图片名称,自动添加后缀png

Returns:
Type
Promise
Example
const findNodeHandle = require('findNodeHandle');
const myMapViewRef = findNodeHandle(this.refs.myMapView);
const imageName = 'mapToShare.png';
let imageToShow = null;
Host.file.amapScreenShot(myMapViewRef, imageName).then(() => {
   imageToShow = <Image source={{local:imageName}}>
   console.log("ok");
});

(static) appendFile(fileName, utf8Content) → {Promise}

向已存在的文件追加内容

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

utf8Content string

文件内容字符串

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.fileappendFile('name', 'base64').then(_ =>{
 //写入成功
 console.log('write success')
})
...

(static) appendFileThroughBase64(fileName, base64Content) → {Promise}

向已存在的文件追加内容,输入为base64编码字符串

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

base64Content string

base64编码后的文件内容字符串

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.fileappendFileThroughBase64('name', 'base64').then(_ =>{
 //写入成功
 console.log('write success')
})
...

(static) dataLengthOfBase64Data(base64Data) → {Promise}

获取 base64 编码的数据长度

Parameters:
Name Type Description
base64Data string

base64 编码的字符串

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
let len = await Host.file.dataLengthOfBase64Data('data')
//or
Host.file.dataLengthOfBase64Data('data').then(len => console.log('len:', len))
...

(static) deleteFile(fileName) → {Promise}

删除文件

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.filedeleteFile('name').then(_ =>{
 console.log('delete success')
})
...

(static) downloadFile(url, fileName) → {Promise}

下载文件到插件沙盒目录

Parameters:
Name Type Description
url string

文件地址

fileName string

存储到本地的文件名

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.file.downloadFile('url', 'targetName').then(res =>{
 console.log('download success with res:', res)
}).catch(err => {
 console.log('download failed with err:', err)
})
...

(static) generateObjNameAndUrlForFDSUpload(did, suffix)

上传普通文件,需要申请权限使用 获取用于上传FDS文件的obj_name以及用于上传的url 设备需要申请配置FDS权限,参考 https://iot.mi.com/new/guide.html?file=08-%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/03-%E5%AD%98%E5%82%A8/01-%E4%BD%BF%E7%94%A8FDS%E5%AD%98%E5%82%A8%E7%94%A8%E6%88%B7%E6%96%87%E4%BB%B6

Parameters:
Name Type Description
did string

设备did

suffix string

文件后缀 例如 'mp3', 'txt'

Since:
  • 10004
Example
let did = Device.deviceID;
        let suffix = "mp3";
        Host.file.generateObjNameAndUrlForFDSUpload(did, suffix).then(res => {
        if (res.hasOwnProperty(suffix) && res[suffix]) {
            let obj = res[suffix];
            let obj_name = obj.obj_name;
            let name = obj_name.substring(obj_name.length - 22)
            let content = "AC";
            let time = obj.time;
            this.file_obj_name = obj_name;
            console.log("pre upload", res)

            Host.file.writeFile(name, content).then(r => {
                let param = {
                    uploadUrl: obj.url,
                    method: obj.method,
                    headers: { "Content-Type": "" },
                    files: [{ filename: name }]
                }
                Host.file.uploadFileToFDS(param).then(rr => {
                    alert('上传成功' + JSON.stringify(rr))
                    console.log('upload file success', rr)
                }).catch(err => {
                    alert('上传失败' + JSON.stringify(err))
                    console.log('upload file failed', err)
                })
            }).catch(err => {
                alert('存储临时文件失败' + JSON.stringify(err))
                console.log("write file failed", err)
            })
        }
        })

(static) generateObjNameAndUrlForLogFileFDSUpload(did, suffix)

上传日志文件。 具体使用参考generateObjNameAndUrlForFDSUpload

Parameters:
Name Type Description
did string
suffix string

string or array

Since:
  • 10011

(static) getFDSFileInfoWithObjName(obj_name)

获取FDS文件的信息,包含下载地址等信息 设备需要申请配置FDS权限,参考 https://iot.mi.com/new/guide.html?file=08-%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/03-%E5%AD%98%E5%82%A8/01-%E4%BD%BF%E7%94%A8FDS%E5%AD%98%E5%82%A8%E7%94%A8%E6%88%B7%E6%96%87%E4%BB%B6

对于手动上传到fds的文件(没有genObjName ,在平台端直接上传的),可直接设置成public,生成url。插件端需要用这个文件时,用通用下载接口下载此url即可。 getFDSFileInfoWithObjName,这个接口只是用来下载通过插件接口(Host.file.uploadFileToFDS)上传到FDS的文件

Parameters:
Name Type Description
obj_name string

generateObjNameAndUrlForFDSUpload 生成的 obj_name

Since:
  • 10004
Example
let did = Device.deviceID;
        let suffix = "mp3";
        let file_obj_name = this.file_obj_name //从服务端获取或者本地获取,通过generateObjNameAndUrlForFDSUpload 生成
        if (file_obj_name) {
        Host.file.getFDSFileInfoWithObjName(file_obj_name).then(res => {
            console.log('getfileurl success', res)
            alert('获取成功' + JSON.stringify(res))
        }).catch(err => {
            console.log('getfileurl failed', err)
        })
        } else {
        alert("先上传文件")
        }

(static) getRGBAValueFromImageAtPath(imagePath, points) → {Promise}

获取图片指定点的色值, 传空数组将返回所有点的色值

Parameters:
Name Type Description
imagePath string

图片文件路径

points Array.<{x:int, y:int}>

位置数组

Returns:
Type
Promise

(static) isFileExists(fileName) → {Promise.<boolean>}

判断文件是否存在

Parameters:
Name Type Description
fileName string

可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

Returns:
Type
Promise.<boolean>
Example
import {Host} from 'miot'
...
let fileExist = await Host.file.isFileExists('fileName')
//or
Host.file.isFileExists('fileName').then(res => {
console.log('file exist at path:', res)
}).catch(err => {
// file name error or get file path with error
})

(static) longScreenShot(viewRef, imageName) → {Promise.<string>}

长截屏,用来截scrollView,会把超出屏幕的部分也截到

Parameters:
Name Type Description
viewRef number

scrollView的引用

imageName string

图片名称,png

Returns:
Type
Promise.<string>
Example
var findNodeHandle = require('findNodeHandle');
 var myScrollView = findNodeHandle(this.refs.myScrollView);
 Host.file.longScreenShot(myScrollView, 'test2.png').then(imagePath=>{
     console.log(imagePath);
 });

(static) readFile(fileName, optopt) → {Promise}

读本地文件

Parameters:
Name Type Attributes Default Description
fileName string

文件名,可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

opt json <optional>
{}

其他设置项

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.filereadFile('name').then(content =>{
 console.log('file content:', content)
})

(static) readFileList() → {Promise}

读取沙盒内文件列表

  • @param {string} subFolder 读取沙盒文件夹下某子文件夹中文件内容,用于解压缩文件中带有文件夹,或者读取指定文件夹解压后的文件,标准path结构,不以'/'开头
Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.file.readFileList().then(res => {
 console.log('read fiel list:', res)
})

Host.file.readFileList('mysubfolder/aaa').then(res => {
 console.log('read fiel list:', res)
})

(static) readFileToBase64(fileName) → {Promise}

读文件,并转换为 Base64 编码

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

Returns:
Type
Promise

(static) readFileToHexString(fileName) → {Promise}

读本地文件

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.filereadFileToHexString('name').then(content =>{
 console.log('file content:', content)
})

(static) saveImageToPhotosAlbum(fileName) → {Promise}

保存指定照片文件到系统相册

Parameters:
Name Type Description
fileName string

可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.file.saveImageToPhotosAlbum('name').then(_ =>{
 console.log('successful save to PhotosAlbum')
})
...

(static) screenShot(imageName) → {Promise.<string>}

屏幕全屏截图

Parameters:
Name Type Description
imageName string

图片名称,png,

Returns:
  • 截图成功回调函数返回存储图片的绝对路径,加载图片时直接使用即可
Type
Promise.<string>
Example
<Image source={{local:imageName, scale:PixelRatio.get()}} />

(static) screenShotInRect(imageName, rect) → {Promise.<string>}

自定义范围的屏幕截图

Parameters:
Name Type Description
imageName string

图片名称,png

rect Object

截屏范围

Returns:
  • 截图成功 返回图片地址
Type
Promise.<string>

(static) subBase64DataOfBase64Data(base64Data, loc, len) → {Promise}

获取一个data的子data(base64编码)

Parameters:
Name Type Description
base64Data string

base64 编码的数据

loc number

起始位置

len number

长度

Returns:
Type
Promise

(static) ungzFile(fileName) → {Promise}

解压缩一个gz文件, 并以base64编码的形式直接返回给插件, 不做本地存储

Parameters:
Name Type Description
fileName string

文件名(插件存储空间内的文件)

Returns:
Type
Promise

(static) ungzYunMiFile(fileName) → {Promise}

为云米扫地机的地图文件解压提供,私有

Parameters:
Name Type Description
fileName string

文件名(插件存储空间内的文件)

Returns:
Type
Promise

(static) unzipFile(fileName) → {Promise}

解压缩一个zip文件,解压缩后的文件会直接存储在插件存储空间的根目录下

Parameters:
Name Type Description
fileName string

文件名(插件存储空间内的文件)

  • @param {string} desitinationPath - 目标解压缩文件夹,默认解压到当前文件夹,如果指定名称,压缩包内容会解压到指定文件夹
Returns:
Type
Promise

(static) uploadFile(params) → {Promise}

上传文件

Parameters:
Name Type Description
params UploadParams

参数字典

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
let params = {
 uploadUrl: 'http://127.0.0.1:3000',
 method: 'POST', // default 'POST',support 'POST' and 'PUT'
 headers: {
     'Accept': 'application/json',
 },
 fields: {
     'hello': 'world',
 },
 files: [
     {
         fileName: 'fileName.png', // 只能上传插件sandbox里的文件
     },
 ]
};
Host.file.uploadFile(params).then(res => {
 console.log('upload success with res:', res)
}).catch(err => {
 console.log('upload failed with err:', err)
})
...

(static) uploadFileToFDS(params) → {Promise}

上传文件到小米云FDS

Parameters:
Name Type Description
params UploadParams

参数字典

Returns:
Type
Promise
Example
same as Host.file.uploadFile

(static) writeFile(fileName, utf8Content) → {Promise}

写文件

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

utf8Content string

文件内容字符串

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.filewriteFile('name', 'content').then(_ =>{
 //写入成功
 console.log('write success')
})
...

(static) writeFileThroughBase64(fileName, base64Content) → {Promise}

写文件,输入为 Base64 编码字符串

Parameters:
Name Type Description
fileName string

文件名, 可以是多重文件夹嵌套文件, e.g 'path/path2/filename.txt'

base64Content string

base64编码后的文件内容字符串

Returns:
Type
Promise
Example
import {Host} from 'miot'
...
Host.filewriteFileThroughBase64('name', 'base64').then(_ =>{
 //写入成功
 console.log('write success')
})
...