This topic describes the JavaScript APIs of WVFile. You can refer to this topic when you create HTML5 apps or MiniApps. The JavaScript APIs of WVFile provide the capabilities to read content from files, write content to files, obtain information about files, download files, and upload files.
WVFile.write
Writes content to a specified file in a disk.
Input parameters
[
string]mode: the mode in which content is written.write: specifies to write content to a new file. If the file does not exist, the file is automatically created. If the file exists, the error messageerror:FILE_EXISTis returned.append: specifies to append content to the end of a file. If the file does not exist, the file is automatically created.overwrite: specifies to overwrite a file. If the file does not exist, the file is automatically created. If the file exists, the file is overwritten.
[
string]data: the content that is written. Specify the path of the file in the following format:Path of WindVane cached files/fileName. If the path does not exist, the path is automatically created.[
string]fileName: the name of the file. If the file does not exist, the file is automatically created. The file name cannot contain a forward slash (/).[
string]share: specifies whether to share the file.
Callback parameters
Callback parameters are passed into the callback method. If content is written to the file, the success callback is invoked. Otherwise, the failure callback is invoked.
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
Reads the content of a specified file.
Input parameters
[
string]fileName: the name of the file that is read. The file name cannot contain a forward slash (/). Specify the path of the file in the following format:Path of WindVane cached files/fileName. If the path does not exist, the error messageerror:PATH_NOT_FOUNDis returned. If the file does not exist, the error messageerror:FILE_NOT_FOUNDis returned.
Callback parameters
Callback parameters are passed into the callback method. If the file is read, the success callback is invoked. Otherwise, the failure callback is invoked.
[
string]data: the content of the specified file.
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
This API is available only in WindVane Android 1.0.X.X or later, WindVane iOS 2.1.4 or later.
Obtains information about a file.
Input parameters
[
string]filePath: the path of the file.
Callback parameters
Parameters for the success callback:
[
string]fileSize: the size of the file.
Parameters for the failure callback:
[
string]msg: the error message.
If the file information is obtained, the success callback is invoked. Otherwise, the failure callback is invoked.
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
This API is available only in WindVane Android 1.0.X.X or later, WindVane iOS 2.1.4 or later.
Downloads a file from a specified URL.
Input parameters
[
string]url: the download URL of the file.[
string]name: the name of the file after the file is downloaded. The default name is timestamp_windvane. We recommend that you specify the name during each download. This parameter is optional.
Callback parameters
Parameters for the success callback:
[
string]filePath: the local storage path of the file after the file is downloaded.
Parameters for the failure callback:
[
string]msg: the error message.
Event listening
WVFile.Event.downloadFileSuccess
The file is downloaded.
Event parameters:
[
string]filePath: the local storage path of the file after the file is downloaded.
WVFile.Event.downloadFileFailed
The file failed to be downloaded.
[
string]msg: the error message.
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
This API is available only in WindVane Android 1.0.X.X or later, WindVane iOS 2.1.4 or later.
Uploads a file to a specified server URL.
Input parameters
[
string]url: the upload URL of the file.[
string]filePath: the storage path of the local file to be uploaded. Make sure that the app has read permissions on the file in this path. If the app does not have such permissions, the file is not uploaded.[
int]timeout: the timeout period. Default value: 6. Unit: seconds. This parameter is optional.[
object]headers: the headers of the upload request.
Callback parameters
Parameters for the success callback:
[
string]data: the data returned by the server after the file is uploaded.[
string]headers: the headers returned by the server after the file is uploaded.
Parameters for the failure callback:
[
string]msg: the error message. If an error occurs during the upload process, the value of msg is a JSON string that contains the following information:[
int]code: the HTTP status code.[
string]data: the data returned by the server.[
string]headers: the headers returned by the server.
Event listening
WVFile.Event.uploadFileSuccess
The file is uploaded.
Event parameters:
[
string]data: the data returned by the server after the file is uploaded.[
string]headers: the headers returned by the server after the file is uploaded.
WVFile.Event.uploadFileFailed
The file failed to be uploaded.
[
string]msg: the error message. If an error occurs during the upload process, the value of msg is a JSON string that contains the following information:[
int]code: the HTTP status code.[
string]data: the data returned by the server.[
string]headers: the headers returned by the server.
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));
});WVFile.chooseFiles
This API is only valid in the following versions.
Android: windvane-mini-app >= 1.6.6, mini-app-adapter >= 1.5.8
iOS: EMASWindVaneMiniApp >= 1.0.9
Select and upload local files.
Input Parameters
None
Callback Parameters
Success Callback Parameters:
callback function will receive an array of data. The key in the array is path, and the value is the local path of the selected file.
Name | Type | Required | Example Value | Description |
files | Array | Yes | select the local path collection of the file |
The files attributes are described as follows:
Name | Type | Required | Example Value | Description |
path | string | Yes | the local path of the selected file |
Failure Callback Parameters:
Name | Type | Required | Example Value | Description |
msg | string | Yes | Fail reason |
Example Code:
window.WindVane.call(
'WVFile',
'chooseFiles',
{},
function(response) {
const files = response.files;
alert("open success: " + JSON.stringify(files));
}, function(e) {
alert("open fail: " + JSON.stringify(e));
}
);WVFile.getDataByFilePath
This API is only valid in the following versions.
Android: windvane-mini-app >= 1.6.6, mini-app-adapter >= 1.5.8
iOS: EMASWindVaneMiniApp >= 1.0.9
Get local file data.
Input Parameters
Name | Type | Required | Example Value | Description |
path | string | /download/dingding/0/1.png | Local file path |
Callback Parameters
Success Callback Parameters:
Name | Type | Required | Example Value | Description |
base64Data | string | Yes | iVBORw0KGgoAAAANSUhEUgAAAAUA.. | File base64 data |
Failure Callback Parameters:
Name | Type | Required | Example Value | Description |
msg | string | Yes | Fail reason |
Example Code:
window.WindVane.call('WVFile', 'getDataByFilePath', { path: '/download/dingding/0/1.png' }, function(data) {
const base64Data = data.base64Data;
alert('getDataByFilePath success: ' + JSON.stringify(data.base64Data));
// Implement logic to send file to the backend service for file saving
...
...
}, function(error) {
alert('getDataByFilePath failure: ' + JSON.stringify(error));
});