文档

本文介绍文件类WVFile相关的JSAPI,供您在通过跨平台DevOps创建H5端应用或者小程序时参考。文件类WVFile的JSAPI提供写入、读取文件内容、获取文件信息、文件下载、文件上传的相关能力。

WVFile.write

将指定的文件内容写入磁盘。

输入参数

  • [string] mode:写入文件的模式。

    • write:表示写文件,若文件不存在则创建文件,若文件已存在则报错:error:FILE_EXIST

    • append:表示在文件末尾追加内容,若文件不存在则创建文件。

    • overwrite:表示覆盖文件,文件不存在则创建文件,如果文件存在则覆盖之前文件的内容。

  • [string] data:待存入文件的内容,文件会存储在WindVane 缓存文件路径/fileName,如果路径不存在,则自动创建路径。

  • [string] fileName:文件名,如果文件不存在,则创建文件(不允许包含"/")。

  • [string] share:是否允许共享。

回调参数

回调参数将会在回调方法中传递,如果写入文件成功,则进入success回调,否则进入failure回调。

var params = {
        mode: 'overwrite',
        data: 'Hello World!!!\n',
        fileName: 'testFile.txt',
        share: 'false'
};
function writeFile () {
        window.WindVane.call('WVFile', 'write', params, function(e) {
                alert('success: ' + JSON.stringify(e));
        }, function(e) {
                alert('failure: ' + JSON.stringify(e));
        });
}

WVFile.read

读取指定文件的内容。

输入参数

  • [string]fileName:要读取的文件名称(不允许包含"/"),文件储存路径为WindVane 缓存文件路径/fileName。如果路径不存在,则返回失败:error:PATH_NOT_FOUND;如果文件不存在,则返回失败:error:FILE_NOT_FOUND

回调参数

回调参数将会在回调方法中传递,如果读取文件成功,则进入success回调,否则进入failure回调。

  • [string]data:读取到的文件内容。

var params = {
        fileName: 'testFile.txt',
        share: 'false'
};
function readFile () {
        window.WindVane.call('WVFile', 'read', params, function(e) {
                success(JSON.stringify(e));
        }, function(e) {
                failure(JSON.stringify(e));
        });
}

WVFile.getFileInfo

说明

该API只在Windvane Android 1.0.3.4以上版本有效。

获取文件信息

输入参数

  • [string]filePath:文件路径。

回调参数

成功回调参数:

  • [string] fileSize:文件大小。

失败回调参数:

  • [string] msg:错误信息。

如果获取文件信息成功,则进入success回调,否则进入failure回调。

var params = {
        filePath: '/storage/emulated/0/Android/data/xxx/testFile.txt',
};
function readFile () {
        window.WindVane.call('WVFile', 'getFileInfo', params, function(e) {
                success(JSON.stringify(e));
        }, function(e) {
                failure(JSON.stringify(e));
        });
}

WVFile.downloadFile

说明

该API只在Windvane Android 1.0.3.4以上版本有效。

下载指定URL的文件

输入参数

  • [string]url:文件下载地址。

  • [string]name:可选,文件下载后在本地的文件名,默认是"时间戳_windvane",建议每次下载时传入name

回调参数

成功回调参数:

  • [string] filePath:下载成功后文件的本地存储地址。

失败回调参数:

  • [string] msg:错误信息。

监听事件

WVFile.Event.downloadFileSuccess

下载文件成功。

事件参数:

  • [string] filePath:下载成功后文件的本地存储地址。

WVFile.Event.downloadFileFailed

下载文件失败

  • [string] msg:错误信息。

document.addEventListener('WVFile.Event.downloadFileSuccess', function (e) {
        alert('event downloadFileSuccess: ' + JSON.stringify(e.param));
});
document.addEventListener('WVFile.Event.downloadFileFailed', function (e) {
        alert('event downloadFileFailed: ' + JSON.stringify(e.param));
});

var params = {
    url: 'http://xxxx',
  	name: 'test.mp4'
};
window.WindVane.call('WVFile', 'downloadFile', params, function(e) {
        alert('downloadFile success: ' + JSON.stringify(e));
}, function(e) {
        alert('downloadFile failure: ' + JSON.stringify(e));
});

WVFile.uploadFile

说明

该API只在Windvane Android 1.0.3.4以上版本有效。

上传文件至指定服务器URL

输入参数

  • [string]url:文件上传地址。

  • [string]filePath:要上传的本地文件存储路径。需要注意的是要确保App对该路径下的文件有读的权限,如果没有权限则不会进行上传。

  • [int]timeout:可选,超时时间,默认是6秒。

  • [object]headers:可选,上传时的Header。

回调参数

成功回调参数:

  • [string] data:上传成功后服务器返回的数据。

  • [string] headers:上传成功后服务器返回的Header。

失败回调参数:

  • [string] msg:错误信息。如果是在上传过程中出错,msg是一个JSON字符串,其中会包含以下信息:

    • [int] code:Http请求状态码。

    • [string] data:服务器返回的数据。

    • [string] headers:服务器返回的Header。

监听事件

WVFile.Event.uploadFileSuccess

上传文件成功。

事件参数:

  • [string] data:上传成功后服务器返回的数据。

  • [string] headers:上传成功后服务器返回的Header。

WVFile.Event.uploadFileFailed

上传文件失败。

  • [string] msg:错误信息。如果是在上传过程中出错,msg是一个JSON字符串,其中会包含以下信息:

    • [int] code:Http请求状态码。

    • [string] data: 服务器返回的数据。

    • [string] headers:服务器返回的Header。

document.addEventListener('WVFile.Event.uploadFileSuccess', function (e) {
        alert('event uploadFileSuccess: ' + JSON.stringify(e.param));
});
document.addEventListener('WVFile.Event.uploadFileFailed', function (e) {
        alert('event uploadFileFailed: ' + JSON.stringify(e.param));
});

var params = {
    url: 'http://xxxx',
  	filePath: '/storage/test.txt',
    timeout: 8000,
    headers: {
       xxx: 'xxx'
    }
};
window.WindVane.call('WVFile', 'uploadFile', params, function(e) {
        alert('uploadFile success: ' + JSON.stringify(e));
}, function(e) {
        alert('uploadFile failure: ' + JSON.stringify(e));
});

  • 本页导读 (0)
文档反馈