全部產品
Search
文件中心

SuperApp:檔案

更新時間:Jun 25, 2025

本文介紹檔案類WVFile相關的JSAPI,供您建立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));
});

WVFile.chooseFiles

說明

該API只在以下版本有效。

Android:windvane-mini-app >= 1.6.6、mini-app-adapter >= 1.5.8

iOS:EMASWindVaneMiniApp >= 1.0.9

選擇上傳本地檔案。

輸入參數

回調參數

成功回調參數:

回呼函數會收到一個數群組類型的資料,數組中的key為path,value為選擇檔案本地路徑,其屬性如下:

名稱

類型

是否必選

樣本值

描述

files

數組

選擇檔案的本地路徑集合

files屬性描述如下:

名稱

類型

是否必選

樣本值

描述

path

字串

選擇檔案的本地路徑

失敗回調參數:

名稱

類型

是否必選

樣本值

描述

msg

字串

失敗原因

範例程式碼如下所示:

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

說明

該API只在以下版本有效。

Android:windvane-mini-app >= 1.6.6、mini-app-adapter >= 1.5.8

iOS:EMASWindVaneMiniApp >= 1.0.9

擷取本地檔案資料。

輸入參數

名稱

類型

是否必選

樣本值

描述

path

字串

/download/dingding/0/1.png

本地檔案路徑

回調參數

成功回調參數:

名稱

類型

是否必選

樣本值

描述

base64Data

字串

iVBORw0KGgoAAAANSUhEUgAAAAUA..

檔案base64資料

失敗回調參數:

名稱

類型

是否必選

樣本值

描述

msg

字串

失敗原因

範例程式碼如下所示:

window.WindVane.call('WVFile', 'getDataByFilePath', { path: '/download/dingding/0/1.png' }, function(data) {
    const base64Data = data.base64Data;
    alert('getDataByFilePath success: ' + JSON.stringify(data.base64Data));
    // 自行實現將formData傳給後端服務進行檔案儲存
    ...
    ...

}, function(error) {
    alert('getDataByFilePath failure: ' + JSON.stringify(error));
});