ionic的file插件用法官方介绍


http://ngcordova.com/docs/plugins/file/


A Plugin to get access to the device’s files and directories.

cordova plugin add cordova-plugin-file

File System Layout

Every OS has a very different FileSystem layout, so here is a detailed description of the FileSystems foriOS and Android

iOS File System Layout
Device Pathcordova.file.*iosExtraFileSystemsr/w?persistent?OS clearssyncprivate
/var/mobile/Applications/<UUID>/applicationStorageDirectory-rN/AN/AN/AYes
    appname.app/applicationDirectorybundlerN/AN/AN/AYes
        www/--rN/AN/AN/AYes
    Documents/documentsDirectorydocumentsr/wYesNoYesYes
        NoCloud/-documents-nosyncr/wYesNoNoYes
    Library-libraryr/wYesNoYes?Yes
        NoCloud/dataDirectorylibrary-nosyncr/wYesNoNoYes
        Cloud/syncedDataDirectory-r/wYesNoYesYes
        Caches/cacheDirectorycacher/wYes*Yes***NoYes
    tmp/tempDirectory-r/wNo**Yes***NoYes

* Files persist across app restarts and upgrades, but this directory can be cleared whenever the OS desires. Your app should be able to recreate any content that might be deleted.

** Files may persist across app restarts, but do not rely on this behavior. Files are not guaranteed to persist across updates. Your app should remove files from this directory when it is applicable, as the OS does not guarantee when (or even if) these files are removed.

*** The OS may clear the contents of this directory whenever it feels it is necessary, but do not rely on this. You should clear this directory as appropriate for your application.

Android File System Layout
Device Pathcordova.file.*AndroidExtraFileSystemsr/w?persistent?OS clearsprivate
file:///android_asset/applicationDirectory rN/AN/AYes
/data/data/<app-id>/applicationStorageDirectory-r/wN/AN/AYes
    cachecacheDirectorycacher/wYesYes*Yes
    filesdataDirectoryfilesr/wYesNoYes
        Documents documentsr/wYesNoYes
<sdcard>/externalRootDirectorysdcardr/wYesNoNo
    Android/data/<app-id>/externalApplicationStorageDirectory-r/wYesNoNo
        cacheexternalCacheDirectorycache-externalr/wYesNo**No
        filesexternalDataDirectoryfiles-externalr/wYesNoNo

* The OS may periodically clear this directory, but do not rely on this behavior. Clear the contents of this directory as appropriate for your application. Should a user purge the cache manually, the contents of this directory are removed.

** The OS does not clear this directory automatically; you are responsible for managing the contents yourself. Should the user purge the cache manually, the contents of the directory are removed.

Note: If external storage can’t be mounted, the cordova.file.external* properties are null.

File Error Codes

When an error is thrown, one of the following codes will be used.

CodeConstant
1NOT_FOUND_ERR
2SECURITY_ERR
3ABORT_ERR
4NOT_READABLE_ERR
5ENCODING_ERR
6NO_MODIFICATION_ALLOWED_ERR
7INVALID_STATE_ERR
8SYNTAX_ERR
9INVALID_MODIFICATION_ERR
10QUOTA_EXCEEDED_ERR
11TYPE_MISMATCH_ERR
12PATH_EXISTS_ERR

Methods

getFreeDiskSpace()

Get the total free disk space on the device.

Returns String

checkDir(path, directory)

Check if a file exists in a certain path, directory.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
directoryStringName of directory to check

Returns Object

checkFile(path, file)

Check if a file exists in a certain path, directory.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to check

Returns Object

createDir(path, directory, replace)

Creates a new directory in the specific path. The replace boolean value determines whether to replace an existing directory with the same name. If an existing directory exists and the replace value is false, the promise will fail and return an error.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
directoryStringName of directory to create
replaceBooleanIf true, replaces file with same name. If false returns error

Returns Object

createFile(path, file, replace)

Creates a new file in the specific path. The replace boolean value determines whether to replace an existing file with the same name. If an existing file exists and the replace value is false, the promise will fail and return an error.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to create
replaceBooleanIf true, replaces file with same name. If false returns error

Returns Object

removeDir(path, directory)

Removes a directory from a desired location.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
directoryStringName of directory to remove

Returns Object

removeFile(path, file)

Removes a file from a desired location.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to remove

Returns Object

removeRecursively(path, directory)

Removes all files and the directory from a desired location.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
directoryStringName of directory to remove

Returns Object

writeFile(path, file, data, replace)

Write to a new file, and replace if desired.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to write to
dataStringText/data to write to the existing file
replaceBooleanIf true, replaces file with same name. If false returns error

Returns Object

writeExistingFile(path, file, data)

Write to an existing file.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to write to
dataStringText/data to write to the existing file

Returns Object

readAsText(path, file)

##### readAsDataURL(path, file) ##### readAsBinaryString(path, file) #####readAsArrayBuffer(path, file)

Read a file in various methods.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to read from

Returns Object

moveDir(path, directory, newPath, newDirectory)

Read a file in various methods.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
directoryStringName of directory to move
pathFileSystemBase FileSystem of new location
newDirectoryStringNew name of directory to move to (leave blank to remain the same)

Returns Object

moveFile(path, file, newPath, newFile)

Read a file in various methods.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to move
pathFileSystemBase FileSystem of new location
fileStringNew name of file to move to (leave blank to remain the same)

Returns Object

copyDir(path, directory, newPath, newDirectory)

Copy a file in various methods. If directory exists, will fail to copy.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
directoryStringName of directory to copy
pathFileSystemBase FileSystem of new location
newDirectoryStringNew name of directory to copy to (leave blank to remain the same)

Returns Object

copyFile(path, file, newPath, newFile)

Read a file in various methods. If file exists, will fail to copy.

ParamTypeDetail
pathFileSystemBase FileSystem. Please refer to the iOS and Android filesystems above
fileStringName of file to copy
pathFileSystemBase FileSystem of new location
fileStringNew name of file to copy to (leave blank to remain the same)

Returns Object

Example

module.controller('MyCtrl', function($scope, $cordovaFile) {

  document.addEventListener('deviceready', function () {

    $cordovaFile.getFreeDiskSpace()
      .then(function (success) {
         // success in kilobytes
      }, function (error) {
          // error
      });


    // CHECK
    $cordovaFile.checkDir(cordova.file.dataDirectory, "dir/other_dir")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    $cordovaFile.checkFile(cordova.file.dataDirectory, "some_file.txt")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    // CREATE
    $cordovaFile.createDir(cordova.file.dataDirectory, "new_dir", false)
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });

    $cordovaFile.createFile(cordova.file.dataDirectory, "new_file.txt", true)
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    // REMOVE
    $cordovaFile.removeDir(cordova.file.dataDirectory, "some_dir")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });

    $cordovaFile.removeFile(cordova.file.dataDirectory, "some_file.txt")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });

    $cordovaFile.removeRecursively(cordova.file.dataDirectory, "")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    // WRITE
    $cordovaFile.writeFile(cordova.file.dataDirectory, "file.txt", "text", true)
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });

    $cordovaFile.writeExistingFile(cordova.file.dataDirectory, "file.txt", "text")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    // READ
    $cordovaFile.readAsText(cordova.file.dataDirectory, $scope.inputs.readFile)
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    // MOVE
    $cordovaFile.moveDir(cordova.file.dataDirectory, "dir", cordova.file.tempDirectory, "new_dir")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });

    $cordovaFile.moveFile(cordova.file.dataDirectory, "file.txt", cordova.file.tempDirectory)
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


    // COPY
    $cordovaFile.copyDir(cordova.file.dataDirectory, "dir", cordova.file.tempDirectory, "new_dir")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });

    $cordovaFile.copyFile(cordova.file.dataDirectory, "file.txt", cordova.file.tempDirectory, "new_file.txt")
      .then(function (success) {
        // success
      }, function (error) {
        // error
      });


  });

});




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值