The WVFile class provides JSAPIs for file operations in H5 applications and mini programs, including write, read, file info retrieval, download, upload, and file selection.
WVFile.write
Writes content to a file on disk.
Parameters
-
[
string] mode: The file writing mode.-
write: Writes to the file. If the file does not exist, WindVane creates it. If the file already exists, WindVane returnserror:FILE_EXIST. -
append: Appends content to the end of the file. If the file does not exist, WindVane creates it. -
overwrite: Overwrites the file. If the file does not exist, WindVane creates it. If the file already exists, its content is overwritten.
-
-
[
string] data: The content to write. The file is stored atWindVane cache file path/fileName. Missing paths are created automatically. -
[
string] fileName: The file name. Cannot contain "/". Created automatically if the file does not exist. -
[
string] share: Whether the file can be shared.
Callback parameters
The success callback is invoked if the file is written successfully. 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.
Parameters
-
[
string] fileName: The file to read. Cannot contain "/". The file is stored atWindVane cache file path/fileName. If the path does not exist, WindVane returnserror:PATH_NOT_FOUND. If the file does not exist, WindVane returnserror:FILE_NOT_FOUND.
Callback parameters
If the file is read successfully, the success callback is invoked. 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 API is available only in WindVane for Android 1.0.3.4 and later.
Retrieves information about a file.
Parameters
-
[
string] filePath: The file path.
Callback parameters
Success callback parameters:
-
[
string] fileSize: The file size.
Failure callback parameters:
-
[
string] msg: The error message.
If the file information is retrieved successfully, 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 for Android 1.0.3.4 and later.
Downloads a file from a specified URL.
Parameters
-
[
string] url: The URL of the file to download. -
[
string] name: Optional. The downloaded file name. Defaults to "timestamp_windvane". Specify a name for each download.
Callback parameters
Success callback parameters:
-
[
string] filePath: The local file path of the downloaded file.
Failure callback parameters:
-
[
string] msg: The error message.
Event listeners
WVFile.Event.downloadFileSuccess
Triggered when the file download is successful.
Event parameters:
-
[
string] filePath: The local file path of the downloaded file.
WVFile.Event.downloadFileFailed
Triggered when the 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://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 for Android 1.0.3.4 and later.
Uploads a file to a specified server URL.
Parameters
-
[
string] url: The server URL to upload the file to. -
[
string] filePath: The local path of the file to upload. Requires read permission; upload fails without it. -
[
int] timeout: Optional. The timeout in milliseconds. The default is 6000. -
[
object] headers: Optional. The request headers for the upload.
Callback parameters
Success callback parameters:
-
[
string] data: The data returned by the server after a successful upload. -
[
string] headers: The headers returned by the server after a successful upload.
Failure callback parameters:
-
[
string] msg: The error message. If an error occurs during the upload, this parameter is 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 the file upload is successful.
Event parameters:
-
[
string] data: The data returned by the server after a successful upload. -
[
string] headers: The headers returned by the server after a successful upload.
WVFile.Event.uploadFileFailed
Triggered when the file upload fails.
-
[
string] msg: The error message. If an error occurs during the upload, this parameter is 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://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 available only in the following versions.
Android: windvane-mini-app 1.6.6 or later, mini-app-adapter 1.5.8 or later.
iOS: EMASWindVaneMiniApp 1.0.9 or later.
Opens a file picker to select local files.
Parameters
None
Callback parameters
Success callback parameters:
The success callback returns an object with these properties:
|
Parameter |
Type |
Required |
Example value |
Description |
|
files |
array |
Yes |
An array of objects, where each object represents a selected file. |
Each object in the files array has the following properties:
|
Parameter |
Type |
Required |
Example value |
Description |
|
path |
string |
Yes |
The local path of the selected file. |
Failure callback parameters:
|
Parameter |
Type |
Required |
Example value |
Description |
|
msg |
string |
Yes |
The reason for failure. |
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 API is available only in the following versions.
Android: windvane-mini-app 1.6.6 or later, mini-app-adapter 1.5.8 or later.
iOS: EMASWindVaneMiniApp 1.0.9 or later.
Gets the Base64-encoded data of a local file.
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 Base64-encoded data of the file. |
Failure callback parameters:
|
Parameter |
Type |
Required |
Example value |
Description |
|
msg |
string |
Yes |
The reason for failure. |
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 own logic to send the formData to your backend service to save the file.
...
...
}, function(error) {
alert('getDataByFilePath failure: ' + JSON.stringify(error));
});