一個 API,用於讀取、 寫入和導航基於w3c api的檔案系統層次結構.

物件

訪問功能

從 3.0 版,科爾多瓦作為外掛程式實現了設備級 Api。 使用 CLI 的 plugin 命令,描述在命令列介面,可以添加或刪除一個專案,為此功能:

    $ cordova plugin add org.apache.cordova.file
    $ cordova plugin ls
    [ 'org.apache.cordova.file' ]
    $ cordova plugin rm org.apache.cordova.file

要使用的檔案傳輸外掛程式必須單獨添加的。

    $ cordova plugin add org.apache.cordova.file-transfer
    $ cordova plugin ls
    [ 'org.apache.cordova.file-transfer' ]
    $ cordova plugin rm org.apache.cordova.file-transfer

這些命令適用于所有有針對性的平臺,但修改如下所述的特定于平臺的配置設置:

一些平臺可能支援此功能,而無需任何特殊的配置。請參見在概述部分中的平臺支援


此物件包含的單個的屬性。

屬性

方法

詳細資訊

File物件包含單個的屬性。您可以獲取的實例 File 物件通過調用 FileEntry 物件的 file() 方法。

支援的平臺

切片

返回一個新的 File 物件,為其 FileReader 返回只有該的指定的部分。 負值設置為 startend 測量從的末尾。 相對於當前切片定位的索引。 (請參閱下面的完整示例)。

參數:

快速的示例

var slicedFile = file.slice(10, 30);

完整的示例

var slice1 = file.slice(100, 400);
var slice2 = slice1.slice(20, 35);

var slice3 = file.slice(120, 135);
// slice2 and slice3 are equivalent.

支援的平臺


FileReader

FileReader允許基本的存取權限。

屬性

注:以下產權不受支援:

方法

詳細資訊

FileReader物件提供的方法來從該設備的檔案系統中讀取檔。 可以讀取,作為文本或 base64 編碼的資料的字串。 事件攔截器接收 loadstartprogressloadloadenderror ,和 abort事件

支援的平臺

readAsDataURL

參數:

快速的示例

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsDataURL(file);
};

var fail = function (error) {
    console.log(error.code);
};

entry.file(win, fail);

readAsText

參數:

快速的示例

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
};

var fail = function (error) {
    console.log(error.code);
};

entry.file(win, fail);

中止

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsText(file);
    reader.abort();
};

function fail(error) {
    console.log(error.code);
}

entry.file(win, fail);

完整的示例

<!DOCTYPE html>
<html>
  <head>
    <title>FileReader Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.file(gotFile, fail);
    }

    function gotFile(file){
        readDataUrl(file);
        readAsText(file);
    }

    function readDataUrl(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as data URL");
            console.log(evt.target.result);
        };
        reader.readAsDataURL(file);
    }

    function readAsText(file) {
        var reader = new FileReader();
        reader.onloadend = function(evt) {
            console.log("Read as text");
            console.log(evt.target.result);
        };
        reader.readAsText(file);
    }

    function fail(error) {
        console.log(error.code);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Read File</p>
  </body>
</html>

iOS 的怪癖

readAsBinaryString

目前僅支援 iOS 和 android 系統。

參數:

快速的示例

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsBinaryString(file);
};

var fail = function (error) {
    console.log(error.code);
};

entry.file(win, fail);

readAsArrayBuffer

目前僅支援 iOS 和 android 系統。

參數:

快速的示例

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(new Uint8Array(evt.target.result));
    };
    reader.readAsArrayBuffer(file);
};

var fail = function (error) {
    console.log(error.code);
};

entry.file(win, fail);

FileWriter

作為物件,它允許您創建和向中寫入資料。

屬性

下面的屬性受支援:

方法

詳細資訊

FileWriter物件提供 utf-8 編碼檔寫入設備檔案系統的方法。 應用程式回應 writestartprogresswritewriteenderror ,和 abort事件

每個 FileWriter 對應于一個中,資料可以被寫入許多倍。 FileWriter維護的 positionlength 屬性,允許到 app seekwrite 檔中的任意位置。 預設情況下, FileWriter 將寫入到中,覆蓋現有資料的開始。 設置可選的 append 到布林 trueFileWriter 的建構函式來寫入到的末尾。

下面列出的所有平臺都支援文本資料。 正在寫入到檔案系統之前文本編碼為 utf-8。 一些平臺還支援可以作為 ArrayBuffer 或 Blob 傳遞中的二進位資料。

支援的平臺

文本和二進位的支援:

僅限文本的支援:

尋求快速的示例

function win(writer) {
    // fast forwards file pointer to end of file
    writer.seek(writer.length);
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

截斷快速示例

function win(writer) {
    writer.truncate(10);
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

寫快速示例

function win(writer) {
    writer.onwrite = function(evt) {
        console.log("write success");
    };
    writer.write("some sample text");
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

二進位檔案寫入快速示例

function win(writer) {
    var data = new ArrayBuffer(5),
        dataView = new Int8Array(data);
    for (i=0; i < 5; i++) {
        dataView[i] = i;
    }
    writer.onwrite = function(evt) {
        console.log("write success");
    };
    writer.write(data);
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

追加快速示例

function win(writer) {
    writer.onwrite = function(evt) {
    console.log("write success");
};
writer.seek(writer.length);
    writer.write("appended text");
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

中止快速示例

function win(writer) {
    writer.onwrite = function(evt) {
        console.log("write success");
    };
    writer.write("some sample text");
    writer.abort();
};

var fail = function(evt) {
    console.log(error.code);
};

entry.createWriter(win, fail);

完整的示例

<!DOCTYPE html>
<html>
  <head>
    <title>FileWriter Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    function gotFileWriter(writer) {
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample text'");
            writer.truncate(11);
            writer.onwriteend = function(evt) {
                console.log("contents of file now 'some sample'");
                writer.seek(4);
                writer.write(" different text");
                writer.onwriteend = function(evt){
                    console.log("contents of file now 'some different text'");
                }
            };
        };
        writer.write("some sample text");
    }

    function fail(error) {
        console.log(error.code);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Write File</p>
  </body>
</html>

檔案系統

此物件表示一個檔案系統

屬性

詳細資訊

FileSystem物件表示檔案系統的資訊。 檔案系統的名稱的公開的檔案系統的清單中是唯一的。 根屬性包含 DirectoryEntry 物件,表示檔案系統的根目錄。

支援的平臺

檔案系統快速示例

function onSuccess(fileSystem) {
    console.log(fileSystem.name);
    console.log(fileSystem.root.name);
}

// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);

完整的示例

<!DOCTYPE html>
<html>
  <head>
    <title>File System Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
    }

    function onFileSystemSuccess(fileSystem) {
        console.log(fileSystem.name);
        console.log(fileSystem.root.name);
    }

    function fail(evt) {
        console.log(evt.target.error.code);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>File System</p>
  </body>
</html>

FileEntry

表示一個檔在檔案系統上, W3C 目錄和系統規範中定義。

屬性

注:下面的屬性由 W3C 規範定義,但不是支援:

方法

支援的平臺

getMetadata

查找有關檔的中繼資料

參數:

快速的示例

函數 success(metadata) {console.log ("上次修改時間:"+ metadata.modificationTime);}函數 fail(error) {alert(error.code);}/ / 請求的中繼資料物件的此條目 entry.getMetadata (成功、 失敗) ;

setMetadata

上一個檔集的中繼資料

目前僅適用于 iOS。

參數:

快速的示例

function success() {
    console.log("The metadata was successfully set.");
}

function fail() {
    alert("There was an error in setting the metadata");
}

// Set the metadata
entry.setMetadata(success, fail, { "com.apple.MobileBackup": 1});

iOS 怪癖

快速的示例

function setFileMetadata(localFileSystem, filePath, metadataKey, metadataValue)
{
    var onSetMetadataWin = function() {
        console.log("success setting metadata")
    }
    var onSetMetadataFail = function() {
        console.log("error setting metadata")
    }

    var onGetFileWin = function(parent) {
        var data = {};
        data[metadataKey] = metadataValue;
        parent.setMetadata(onSetMetadataWin, onSetMetadataFail, data);
    }
    var onGetFileFail = function() {
        console.log("error getting file")
    }

    var onFSWin = function(fileSystem) {
        fileSystem.root.getFile(filePath, {create: true, exclusive: false}, onGetFileWin, onGetFileFail);
    }

    var onFSFail = function(evt) {
        console.log(evt.target.error.code);
    }

    window.requestFileSystem(localFileSystem, 0, onFSWin, onFSFail);
}

    setFileMetadata(LocalFileSystem.PERSISTENT, "Backups/sqlite.db", "com.apple.MobileBackup", 1);

moveTo

在檔案系統上的檔移到不同的位置。如果應用程式嘗試向會導致錯誤:

此外,在現有檔中移動檔將嘗試刪除和替換該

參數:

快速的示例

function success(entry) {
    console.log("New Path: " + entry.fullPath);
}

function fail(error) {
    alert(error.code);
}

function moveFile(entry) {
    var parent = document.getElementById('parent').value,
        parentName = parent.substring(parent.lastIndexOf('/')+1),
        parentEntry = new DirectoryEntry(parentName, parent);

    // move the file to a new directory and rename it
    entry.moveTo(parentEntry, "newFile.txt", success, fail);
}

copyTo

將檔案複製到檔案系統上的新位置。如果應用程式嘗試向會導致錯誤:

參數:

快速的示例

function win(entry) {
    console.log("New Path: " + entry.fullPath);
}

function fail(error) {
    alert(error.code);
}

function copyFile(entry) {
    var parent = document.getElementById('parent').value,
        parentName = parent.substring(parent.lastIndexOf('/')+1),
        parentEntry = new DirectoryEntry(parentName, parent);

    // copy the file to a new directory and rename it
    entry.copyTo(parentEntry, "file.copy", success, fail);
}

toURL

返回一個可用於查找的的 URL。

快速的示例

// Request the URL for this entry
var fileURL = entry.toURL();
console.log(fileURL);

刪除

刪除的

參數:

快速的示例

function success(entry) {
    console.log("Removal succeeded");
}

function fail(error) {
    alert('Error removing file: ' + error.code);
}

// remove the file
entry.remove(success, fail);

getParent

查找父 DirectoryEntry 包含該

參數:

快速的示例

function success(parent) {
    console.log("Parent Name: " + parent.name);
}

function fail(error) {
    alert(error.code);
}

// Get the parent DirectoryEntry
entry.getParent(success, fail);

createWriter

創建 FileWriter 物件與所代表的關聯FileEntry.

參數:

快速的示例

function success(writer) {
    writer.write("Some text to the file");
}

function fail(error) {
    alert(error.code);
}

// create a FileWriter to write to the file
entry.createWriter(success, fail);

返回 File 物件,它表示該的目前狀態,這 FileEntry 表示。

參數:

快速的示例

function success(file) {
    console.log("File size: " + file.size);
}

function fail(error) {
    alert("Unable to retrieve file properties: " + error.code);
}

// obtain properties of a file
entry.file(success, fail);

枚舉指定工作組或

此物件表示一個目錄在檔案系統上,如由W3C 目錄和系統規範定義的。

屬性

注:下面的屬性由 W3C 規範定義,但不是支援:

方法

下面的方法可以上調用 DirectoryEntry 物件:

支援的平臺

getMetadata

查找有關目錄的中繼資料

參數:

快速的示例

函數 success(metadata) {console.log ("上次修改時間:"+ metadata.modificationTime);}函數 fail(error) {alert(error.code);}/ / 請求的中繼資料物件的此條目 entry.getMetadata (成功、 失敗) ;

setMetadata

設置目錄的擴展的屬性或中繼資料目前僅適用于 iOS。

參數:

快速的示例

function success() {
    console.log("The metadata was successfully set.");
}

function fail() {
    alert("There was an error in setting the metadata");
}

// Set the metadata
entry.setMetadata(success, fail, { "com.apple.MobileBackup": 1});

iOS 怪癖

快速的示例

function setFolderMetadata(localFileSystem, subFolder, metadataKey, metadataValue)
{
    var onSetMetadataWin = function() {
        console.log("success setting metadata")
    }
    var onSetMetadataFail = function() {
        console.log("error setting metadata")
    }

    var onGetDirectoryWin = function(parent) {
        var data = {};
        data[metadataKey] = metadataValue;
        parent.setMetadata(onSetMetadataWin, onSetMetadataFail, data);
    }
    var onGetDirectoryFail = function() {
        console.log("error getting dir")
    }

    var onFSWin = function(fileSystem) {
        fileSystem.root.getDirectory(subFolder, {create: true, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
    }

    var onFSFail = function(evt) {
        console.log(evt.target.error.code);
    }

    window.requestFileSystem(localFileSystem, 0, onFSWin, onFSFail);
}

    setFolderMetadata(LocalFileSystem.PERSISTENT, "Backups", "com.apple.MobileBackup", 1);

moveTo

移動到不同的位置在檔案系統上的目錄。如果應用程式嘗試向會導致錯誤:

移動目錄上現有的空目錄嘗試刪除並替換為該目錄。

參數:

快速的示例

function success(entry) {
    console.log("New Path: " + entry.fullPath);
}

function fail(error) {
    alert(error.code);
}

function moveDir(entry) {
    var parent = document.getElementById('parent').value,
        parentName = parent.substring(parent.lastIndexOf('/')+1),
        newName = document.getElementById('newName').value,
        parentEntry = new DirectoryEntry(parentName, parent);

    // move the directory to a new directory and rename it
    entry.moveTo(parentEntry, newName, success, fail);
}

copyTo

將一個目錄複寫到檔案系統上的不同位置。如果應用程式嘗試向會導致錯誤:

目錄副本始終是遞迴的並將複製的目錄的所有內容。

參數:

快速的示例

function win(entry) {
    console.log("New Path: " + entry.fullPath);
}

function fail(error) {
    alert(error.code);
}

function copyDir(entry) {
    var parent = document.getElementById('parent').value,
        parentName = parent.substring(parent.lastIndexOf('/')+1),
        newName = document.getElementById('newName').value,
        parentEntry = new DirectoryEntry(parentName, parent);

    // copy the directory to a new directory and rename it
    entry.copyTo(parentEntry, newName, success, fail);
}

toURL

返回一個可以用來找到的目錄的 URL。

快速的示例

// Get the URL for this directory
var dirURL = entry.toURL();
console.log(dirURL);

刪除

刪除一個目錄。如果應用程式嘗試向會導致錯誤:

參數:

快速的示例

函數 success(entry) {console.log ("刪除成功");}函數 fail(error) {警報 (' 刪除目錄時出錯: ' + error.code);}/ / 刪除此目錄 entry.remove (成功、 失敗) ;

getParent

查找父 DirectoryEntry 包含的目錄。

參數:

快速的示例

function success(parent) {
    console.log("Parent Name: " + parent.name);
}

function fail(error) {
    alert('Failed to get parent directory: ' + error.code);
}

// Get the parent DirectoryEntry
entry.getParent(success, fail);

createReader

創建新的 DirectoryReader 來讀取目錄中的條目。

快速的示例

// create a directory reader
var directoryReader = entry.createReader();

getDirectory

創建或查找現有的目錄。如果應用程式嘗試向會導致錯誤:

參數:

快速的示例

函數 success(dirEntry) {console.log ("目錄名稱:"+ dirEntry.name);}函數 fail(error) {警報 ("無法創建新的目錄:"+ error.code);}/ / 檢索現有的目錄,或創建它,如果它不存在 entry.getDirectory ("newDir",{創建: true,獨家: false},成功,失敗) ;

getFile

創建或查找。如果應用程式嘗試向會導致錯誤:

參數:

快速的示例

函數 success(fileEntry) {console.log ("的名稱:"+ fileEntry.name);}函數 fail(error) {警報 ("未能檢索:"+ error.code);}/ / 檢索現有的,或創建它,如果它不存在 entry.getFile ("newFile.txt",{創建: true,獨家: false},成功,失敗) ;

removeRecursively

刪除一個目錄和其所有內容。 如果發生錯誤 (例如試圖刪除包含一個不能被刪除的的目錄),可能會刪除一些目錄的內容。 如果應用程式嘗試向會導致錯誤:

參數:

快速的示例

function success(parent) {
    console.log("Remove Recursively Succeeded");
}

function fail(error) {
    alert("Failed to remove directory or it's contents: " + error.code);
}

// remove the directory and all it's contents
entry.removeRecursively(success, fail);

黑莓手機的怪癖

可能會失敗, ControlledAccessException 在以下情況中:

解決方案: 確保手動,或由前重新安裝應用程式的臨時目錄,清潔。

解決方案: 從設備拔下 USB 電纜,然後再次運行。


DirectoryReader

列出和目錄在目錄中,如W3C 目錄和系統規範中定義的物件。

方法

支援的平臺

readEntries

讀取此目錄中的項。

參數:

快速的示例

function success(entries) {
    var i;
    for (i=0; i<entries.length; i++) {
        console.log(entries[i].name);
    }
}

function fail(error) {
    alert("Failed to list directory contents: " + error.code);
}

// Get a directory reader
var directoryReader = dirEntry.createReader();

// Get a list of all the entries in the directory
directoryReader.readEntries(success,fail);

檔案傳輸

FileTransfer物件允許你上傳或下載案,伺服器和用戶端。

屬性

方法

詳細資訊

FileTransfer物件提供一種方法將上載到遠端伺服器使用多部分的 HTTP POST 請求。 支援 HTTP 和 HTTPS 協定。 可以通過指定可選參數 FileUploadOptions 物件的 upload() 方法。 上傳成功, FileUploadResult 物件傳遞給成功回。 如果發生錯誤, FileTransferError 物件傳遞到錯誤回。 它也是可能的 (只在 iOS 和 Android) 從遠端伺服器下載檔案並將其保存在設備上。

支援的平臺

上傳

參數:

快速的示例

// !! Assumes variable fileURI contains a valid URI to a text file on the device

var win = function (r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

var fail = function (error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "text/plain";

var params = {};
params.value1 = "test";
params.value2 = "param";

options.params = params;

var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);

完整的示例

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>File Transfer Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for device API libraries to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // device APIs are available
        //
        function onDeviceReady() {
            // Retrieve image file location from specified source
            navigator.camera.getPicture(
                uploadPhoto,
                function(message) { alert('get picture failed'); },
                {
                    quality         : 50,
                    destinationType : navigator.camera.DestinationType.FILE_URI,
                    sourceType      : navigator.camera.PictureSourceType.PHOTOLIBRARY
                }
            );
        }

        function uploadPhoto(imageURI) {
            var options = new FileUploadOptions();
            options.fileKey="file";
            options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType="image/jpeg";

            var params = {};
            params.value1 = "test";
            params.value2 = "param";

            options.params = params;

            var ft = new FileTransfer();
            ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
        }

        function win(r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
        }

        function fail(error) {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        }

        </script>
</head>
<body>
    <h1>Example</h1>
    <p>Upload File</p>
</body>
</html>

設置上傳標頭

在 Android 和 iOS 上受支援

function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

function fail(error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

var uri = encodeURI("http://some.server.com/upload.php");

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=fileURI.substr(fileURI.lastIndexOf('/')+1);
options.mimeType="text/plain";

var headers={'headerParam':'headerValue'};

options.headers = headers;

var ft = new FileTransfer();
ft.upload(fileURI, uri, win, fail, options);

Android 的怪癖

設置 chunkedMode 選項 false ,防止將上載到 Nginx 伺服器的問題。

下載

參數:

快速的示例

// !! 假定檔路徑是設備 var 檔案傳輸的有效路徑 = 新 FileTransfer() ;var uri = encodeURI ("HTTP://some.server.com/download.php") ;fileTransfer.download (uri,路徑,function(entry) {console.log ("下載完成:"+ entry.fullPath) ;},function(error) {console.log ("下載錯誤源"+ error.source) ;console.log ("下載錯誤目標"+ error.target) ;console.log ("上傳錯誤代碼"+ error.code) ;},false,{標題: {"授權書":"基本 dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA = ="}}) ;

中止

中止正在進行轉讓。Onerror 回檔傳遞的錯誤代碼為 FileTransferError.ABORT_ERR 的 FileTransferError 物件。

支援的平臺

快速的示例

// !! 假定變數 fileURI 包含有效的 URI 到一個文字檔中,對設備無功贏 = function(r) {console.log ("不應調用。");}var 失敗 = function(error) {/ / error.code = = FileTransferError.ABORT_ERR 警報 ("發生了一個錯誤: 代碼 ="+ error.code) ;console.log ("上傳錯誤源"+ error.source) ;console.log ("上傳錯誤目標"+ error.target);}var 選項 = 新 FileUploadOptions() ;options.fileKey="file";options.fileName="myphoto.jpg";options.mimeType="image/jpeg";var ft = 新 FileTransfer() ;ft.upload (fileURI、 encodeURI ("HTTP://some.server.com/upload.php")、 贏、 失敗、 選項) ;ft.abort() ;

onprogress

每當新的資料區塊轉移與 ProgressEvent 調用。

支援的平臺

示例

fileTransfer.onprogress = function(progressEvent) {
    if (progressEvent.lengthComputable) {
      loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
    } else {
      loadingStatus.increment();
    }
};
fileTransfer.download(...); // or fileTransfer.upload(...);

怪癖-在這兩個一個 iOS,Android 上 lengthComputable 是 false 使用 gzip 已編碼的下載。


FileUploadOptions

A FileUploadOptions 可以將物件傳遞給 FileTransfer 物件的 upload() 方法,以指定上載腳本的附加參數。

屬性

說明

A FileUploadOptions 可以將物件傳遞給 FileTransfer 物件的 upload() 方法,以指定上載腳本的附加參數。

WP7 怪癖


FileUploadResult

A FileUploadResult 物件傳遞給成功回FileTransfer 物件的 upload() 方法。

屬性

說明

FileUploadResult通過成功回的返回的物件是 FileTransfer 物件的 upload() 方法。

iOS 的怪癖


標誌

提供的參數 DirectoryEntry 物件的 getFile()getDirectory() 方法,查找或創建的和目錄,分別。

屬性

支援的平臺

快速的示例

/ / 獲取資料目錄,如果它不存在,則創建它。
dataDir = fileSystem.root.getDirectory ("資料",{創建: true});/ / 創建鎖定檔,當且僅當它不存在。
備份的鎖定 = dataDir.getFile ("lockfile.txt",{創建: 真實、 獨家: true}) ;

場合

此物件提供一個獲取根檔案系統方法。

方法

常量

詳細資訊

LocalFileSystem物件的方法定義在 window 物件。

支援的平臺

請求檔案系統快速示例

function onSuccess(fileSystem) {
    console.log(fileSystem.name);
}

// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

解決本地檔案系統的 URI 快速示例

function onSuccess(fileEntry) {
    console.log(fileEntry.name);
}

window.resolveLocalFileSystemURI("file:///example.txt", onSuccess, onError);

完整的示例

<!DOCTYPE html>
<html>
  <head>
    <title>Local File System Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
        window.resolveLocalFileSystemURI("file:///example.txt", onResolveSuccess, fail);
    }

    function onFileSystemSuccess(fileSystem) {
        console.log(fileSystem.name);
    }

    function onResolveSuccess(fileEntry) {
        console.log(fileEntry.name);
    }

    function fail(evt) {
        console.log(evt.target.error.code);
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Local File System</p>
  </body>
</html>

requestFileSystem

請求一個檔案系統,用來存儲應用程式資料。

 window.requestFileSystem(type, size, successCallback, errorCallback)

請求檔案系統快速示例

function onSuccess(fileSystem) {
    console.log(fileSystem.name);
}

// request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, onError);

中繼資料

供應狀態資訊的或目錄的一個介面。

屬性

詳細資訊

Metadata物件表示或目錄的狀態資訊。 調用 DirectoryEntryFileEntry 物件的 getMetadata() 方法將產生 Metadata 實例。

支援的平臺

快速的示例

function win(metadata) {
    console.log("Last Modified: " + metadata.modificationTime);
}

// Request the metadata object for this entry
entry.getMetadata(win, null);

FileError

A FileError 物件時出現錯誤在 API 方法中的任何設置。

屬性

常量

說明

FileError物件是提供給任何 API 錯誤回的唯一參數。 若要確定錯誤的類型,比較其 code 屬性設置為任何上述的節目表。


FileTransferError

A FileTransferError 物件傳遞到錯誤回時出現錯誤。

屬性

常量

說明

FileTransferError上傳或下載檔案時出現錯誤時,將物件傳遞到錯誤回