LoginSignup
11
8

More than 5 years have passed since last update.

[Monaca] Fileプラグインについて

Posted at

Monacaでファイル操作を行う場合は、Fileプラグインを利用します。
Fileプラグインは、Monacaの基本Cordovaプラグインに含まれており、Basicプランから利用することができます。

Fileプラグインの使い方

まず初めに、対象のプロジェクトの「Cordovaプラグインの管理」から、Fileプラグインを有効にします。
プロジェクトでFileプラグインが利用できるのは、devicereadyイベント発行後になります。

<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    alert("Fileプラグインが利用できます。");
}
</script>

requestFileSystemとresolveLocalFileSystemURI

メソッド名 用途
requestFileSystem アプリケーションのデータを格納するファイルシステムを取得します。
持続的:window.PERSISTENT
一時的:window.TEMPORARY
resolveLocalFileSystemURI 指定されたローカルURIに対してのファイルシステムを取得します。

requestFileSystemで取得できるファイルシステムでは、下記を取得することができます。

プロパティー名 プロパティーの中身
root ファイルシステムのルートディレクトリのDirectoryEntryオブジェクト。
name ファイルシステムの名前。

ファイルの一覧を取得する

LocalFileSystem.PERSISTENTにあるファイルの一覧は、下記で取得することができます。
コードは、下記になります。

<script>
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail);
        //window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, onFileSystemSuccess, onFileSystemFail);
    }
    function onFileSystemSuccess(fileSystem) {
        var directoryEntry = fileSystem.root;
        var directoryReader = directoryEntry.createReader();
        directoryReader.readEntries(getFileName, fail);
    }
    function onFileSystemFail(error) {
        console.log("error: " + error.code);
    }

    function getFileName(fileEntries) {
        for (var index = 0; index < fileEntries.length; index++) {
            console.log(fileEntries[index].name)
        }
    }
    function fail(error) {
        console.log("error: " + error.code);
    }
</script>

ファイルを移動する

resolveLocalFileSystemURLで取得できるFileEntryには、ファイル操作をするための様々なメソッドが用意されています。その中のmoveTo()を使って、LocalFileSystem.TEMPORARYにあるファイルをLocalFileSystem.PERSISTENTへ移動してみます。
コードは、下記になります。

<script>
    var persistentDirectoryEntry;
    var temporaryDirectoryEntry;

    // サンプルパス
    var targetFilePath = "cdvfile://localhost/temporary/sample.jpg";

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onPersistentFileSystemSuccess, onPersistentFileSystemFail);
        window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, onTemporaryFileSystemSuccess, onTemporaryFileSystemFail);
    }

    // LocalFileSystem.PERSISTENTコールバック
    function onPersistentFileSystemSuccess(fileSystem) {
        persistentDirectoryEntry = fileSystem.root;
    }
    function onPersistentFileSystemFail(error) {
        console.log('onPersistentFileSystemFail: ' + error.code);
    }

    // LocalFileSystem.TEMPORARYコールバック
    function onTemporaryFileSystemSuccess(fileSystem) {
        temporaryDirectoryEntry = fileSystem.root;

        // 対象のファイルを取得
        window.resolveLocalFileSystemURL(targetFilePath, onResolveSuccess, onResolveFail);
    }
    function onTemporaryFileSystemFail(error) {
        console.log("onTemporaryFileSystemFail: " + error.code);
    }

    // resolveLocalFileSystemURLコールバック
    function onResolveSuccess(fileEntry) {
        // PERSISTENTへ移動。
        fileEntry.moveTo(persistentDirectoryEntry, fileEntry.name, moveToSuccess, moveToFail);
        function moveToSuccess(fileEntry) {
            console.log("" + fileEntry.name + "」を移動しました。");
        }
        function moveToFail(error) {
            console.log("moveToFail: " + error.code);
        }
    }
    function onResolveFail(error) {
        console.log("onResolveFail: " + error.code);
    }
</script>

FileEntryやDirectoryEntryには、moveTo()以外にも、コピーをするためのcopyTo()や、削除をするためのremove()なども用意されています。これらを組み合わせることにより、さまざまな処理を行うことができます。

11
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8