This topic describes the JavaScript APIs (JSAPIs) for the WVFile class, which you can use as a reference when creating H5 applications or mini programs. The WVFile JSAPIs let you write and read file content, get file information, and download and upload files.
WVFile.write
Writes specified file content to disk.
Input parameters
[
string] mode: The file writing mode.write: Writes to a file. If the file does not exist, it is created. If the file already exists, anerror:FILE_EXISTerror is returned.append: Appends content to the end of a file. If the file does not exist, it is created.overwrite: Overwrites a file. If the file does not exist, it is created. If the file exists, its content is overwritten.
[
string] data: The content to write to the file. The API saves the file toWindVane cache file path/fileName. If the path does not exist, the API automatically creates it.[
string] fileName: The file name. A file is created if it does not exist. The file name cannot contain forward slashes (/).[
string] share: Specifies whether the file can be shared.
Callback parameters
The success callback is invoked upon a successful write. 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 to read. The file name cannot contain forward slashes (/). The file must be located in theWindVane cache file path/fileNamedirectory. If the path does not exist, anerror:PATH_NOT_FOUNDerror is returned. If the file does not exist, anerror:FILE_NOT_FOUNDerror is returned.
Callback parameters
The success callback is invoked upon a successful read. Otherwise, the failure callback is invoked.
[
string] data: The content read from the 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 JSAPI is available only in WindVane for Android 1.0.3.4 and later.
Gets file information.
Input parameters
[
string] filePath: The file path.
Callback parameters
Success callback parameters:
[
string] fileSize: The file size.
Failure callback parameters:
[
string] msg: The error message.
The success callback is invoked upon successful retrieval of the file information. 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 JSAPI is available only in WindVane for Android 1.0.3.4 and later.
Downloads a file from a specified URL.
Input parameters
[
string] url: The file download URL.[
string] name: Optional. The local name for the downloaded file. The default value is "timestamp_windvane". We recommend that you specify a name for each download.
Callback parameters
Success callback parameters:
[
string] filePath: The local storage path of the downloaded file.
Failure callback parameters:
[
string] msg: The error message.
Event listeners
WVFile.Event.downloadFileSuccess
Triggered when a file is downloaded successfully.
Event parameters:
[
string] filePath: The local storage path of the downloaded file.
WVFile.Event.downloadFileFailed
Triggered when a file download fails.
[
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://example.com/test.mp4',
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 JSAPI is available only in WindVane for Android 1.0.3.4 and later.
Uploads a file to a specified server URL.
Input parameters
[
string] url: The URL to which the file will be uploaded.[
string] filePath: The local storage path of the file to upload. Your application must have read permissions for the file at this path. The upload will not start if permission is not granted.[
int] timeout: Optional. The timeout in milliseconds. The default value is 6000.[
object] headers: Optional. The headers to include in the upload request.
Callback parameters
Success callback parameters:
[
string] data: The data returned by the server upon a successful upload.[
string] headers: The headers returned by the server upon a successful upload.
Failure callback parameters:
[
string] msg: An error message. If an error occurs during the upload,msgis a JSON string that contains the following fields:[
int] code: The HTTP status code.[
string] data: The data returned by the server.[
string] headers: The headers returned by the server.
Event listeners
WVFile.Event.uploadFileSuccess
Triggered when a file is uploaded successfully.
Event parameters:
[
string] data: The data returned by the server upon a successful upload.[
string] headers: The headers returned by the server upon a successful upload.
WVFile.Event.uploadFileFailed
Triggered when a file upload fails.
[
string] msg: An error message. If an error occurs during the upload,msgis a JSON string that contains the following fields:[
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://example.com/upload',
filePath: '/storage/test.txt',
timeout: 8000,
headers: {
key: 'value'
}
};
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 JSAPI is available only in the following versions.
Android: windvane-mini-app >= 1.6.6, mini-app-adapter >= 1.5.8
iOS: EMASWindVaneMiniApp >= 1.0.9
Allows you to select local files.
Input parameters
Parameter | Type | Required | Example value | Description | Notes |
multiple | boolean | No | true/false | Specifies whether to allow multiple file selection. If | Android: windvane-mini-app >= 1.8.17.5, mini-app-adapter >= 1.8.17.5 iOS: EMASWindVaneMiniApp >= 1.2.9.2 |
Callback parameters
Success callback parameters:
The success callback receives an object with the following parameter:
Parameter | Type | Required | Example value | Description |
files | array | Yes | An array of objects representing the selected files. |
The files array contains objects, each with the following property:
Parameter | Type | Required | Example value | Description |
path | string | Yes | The local path of a selected file. |
Failure callback parameters:
Parameter | Type | Required | Example value | Description |
msg | string | Yes | The failure reason. |
Example:
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 JSAPI is available only in the following versions.
Android: windvane-mini-app >= 1.6.6, mini-app-adapter >= 1.5.8
iOS: EMASWindVaneMiniApp >= 1.0.9
Gets local file data.
Input parameters
Parameter | Type | Required | Example value | Description |
path | string | Yes | /download/dingding/0/1.png | The local file path. |
Callback parameters
Success callback parameters:
Parameter | Type | Required | Example value | Description |
base64Data | string | Yes | iVBORw0KGgoAAAANSUhEUgAAAAUA.. | The file base64 data. |
Failure callback parameters:
Parameter | Type | Required | Example value | Description |
msg | string | Yes | The failure reason. |
Example:
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 your logic to send the data to your backend server to save the file.
...
...
}, function(error) {
alert('getDataByFilePath failure: ' + JSON.stringify(error));
});