目录

File Storage

Data processed by the quick app APIs may be read by other apps when being used on a PC. Therefore, you are not advised to store sensitive data through those APIs.

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

{"name": "system.file"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

import file from '@system.file'

Or

var file = require("@system.file")

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, Vision products, and telematics devices

Applicable regions

Global

Definition

API

Description

file.move(OBJECT)

Moves the source file to a specified location.

file.copy(OBJECT)

Copies the source file and saves it to a specified directory.

file.list(OBJECT)

Obtains a file list in a specified directory.

file.get(OBJECT)

Obtains information about a local file.

file.delete(OBJECT)

Deletes a local file.

file.writeText(OBJECT)

Writes text into a specified file.

file.writeArrayBuffer(OBJECT)

Writes content into the buffer.

file.readArrayBuffer(OBJECT)

Reads data from the buffer.

file.readText(OBJECT)

Reads text.

file.access(OBJECT)

Checks whether a file or directory exists.

file.mkdir(OBJECT)

Creates a directory.

file.rmdir(OBJECT)

Deletes a directory.

file.copyFileToPublicDir(OBJECT)

Copies a file to the public system directory.

[h2]file.move(OBJECT)

Description

Moves the source file to a specified location.

OBJECT

Parameter

Type

Mandatory

Description

srcUri

string

Yes

URI of the source file, which cannot be the path of app resources or be of the tmp type.

For details about the URI, please refer to File Structure > URI.

dstUri

string

Yes

URI of the target file, which cannot be the path of app resources or be of the tmp type.

For details about the URI, please refer to File Structure > URI.

success

function

No

Success callback. The URI of the target file will be returned.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.move({
  srcUri: "internal://cache/path/to/file",
  dstUri: "internal://files/path/to/file",
  success: function(uri) {
    console.log("move success: " + uri);
  },
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
})

[h2]file.copy(OBJECT)

Description

Copies the source file and saves it to a specified directory.

OBJECT

Parameter

Type

Mandatory

Description

srcUri

string

Yes

URI of the source file. If an in-app resource path is used, it must be an absolute path.

For details about the URI, please refer to File Structure > URI.

dstUri

string

Yes

URI of the target file, which cannot be the path of app resources or be of the tmp type.

For details about the URI, please refer to File Structure > URI.

success

function

No

Success callback. The URI of the target file will be returned.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.copy({
  srcUri: "internal://cache/path/to/file",
  dstUri: "internal://files/path/to/file",
  success: function(uri) {
    console.log("copy success: " + uri);
  },
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
})

[h2]file.list(OBJECT)

Description

Obtains a file list in a specified directory.

OBJECT

Parameter

Type

Mandatory

Description

uri

string

Yes

URI of the directory.

For details about the URI, please refer to File Structure > URI.

NOTE:
  • The URI cannot be of the tmp type.
  • The URI can be an absolute path or a relative path if it is the path of app resources.

success

function

No

Callback upon success. {fileList:[{uri:'file1',lastModifiedTime:1234456, length:123456} ...]} will be returned.

For details about the return values, please refer to Parameters returned in success. For details about the meta information about each file in the return value, please refer to File information.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

fileList

array

File list. Each file is in the {uri:'file1',lastModifiedTime:1234456, length:123456} format.

File information

Parameter

Type

Description

uri

string

File URI, which can be accessed by other components or features.

For details about the URI, please refer to File Structure > URI.

length

number

File size, in bytes.

lastModifiedTime

number

Timestamp for file storage, which is the number of milliseconds elapsed since 1970-01-01 00:00:00 GMT.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.list({
  uri: "internal://files/movies/"
  success: function(data) {
    console.log(data.fileList)
  },
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
})

[h2]file.get(OBJECT)

Description

Obtains information about a local file.

OBJECT

Parameter

Type

Mandatory

Description

uri

string

Yes

File URI.

For details about the URI, please refer to File Structure > URI.

NOTE:

The URI can be an absolute path or a relative path if it is the path of app resources.

recursive (1060+)

boolean

No

Indicates whether to recursively obtain the file list in a subdirectory. The default value is false.

success

function

No

Callback upon success. {uri:'file1', length:123456, lastModifiedTime:1233456} will be returned.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

uri

string

File URI, which can be accessed by other components or features.

For details about the URI, please refer to File Structure > URI.

length

number

File size, in bytes.

lastModifiedTime

number

Timestamp for file storage, which is the number of milliseconds elapsed since 1970-01-01 00:00:00 GMT.

type (1060+)

string

File type.

  • dir: directory
  • file: file

subFiles (1060+)

string

File list. When recursive is set to true and type is set to dir, the file information in the subdirectory will be recursively returned. Otherwise, no value will be returned.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.get({
  uri: 'internal://files/path/to/file',
  success: function(data) {
    console.log(data.uri);
    console.log(data.length);
    console.log(data.lastModifiedTime);
  },
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
})

[h2]file.delete(OBJECT)

Description

Deletes a local file.

OBJECT

Parameter

Type

Mandatory

Description

uri

string

Yes

URI of the file to be deleted, which cannot be the path of app resources or be of the tmp type.

For details about the URI, please refer to File Structure > URI.

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.delete({
  uri: 'internal://files/path/to/file',
  success: function(data) {
    console.log("handling success");
  },
  fail: function(data, code) {
    console.log("handling fail, code=" + code);
  }
})

[h2]file.writeText(OBJECT) (1010+)

Description

Writes text into a specified file.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

Local file path, which must not be a resource file path or of the tmp type. If no local file exists, a file will be created in the path internal://cache/*, internal://files/*, or internal://mass/*.

For details about the URI, please refer to File Structure > URI.

text

string

-

Yes

Text string to be written into a local file.

encoding

string

UTF-8

No

Encoding format.

append (1060+)

boolean

false

No

Indicates whether to enable the append mode.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.writeText({
    uri: 'internal://cache/test/test.txt',
    text: 'nihaadpafsafa fsafass faf  sfsa sf',
    encoding: 'UTF-8',
    success: function() {
        console.log('success');               
    },
    fail: function(errmsg, errcode) {
        console.log('failed ' + errmsg+' errcode = '+errcode);
   }
})

[h2]file.writeArrayBuffer(OBJECT) (1010+)

Description

Writes content into the buffer.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

URI of a local file, which cannot be the path of app resources or be of the tmp type. A file will be created if no local file exists.

For details about the URI, please refer to File Structure > URI.

buffer

uint8array

-

Yes

Text string to be written into a local file. For details, please refer to Uint8Array.

position

number

0

No

Position where the writing starts. If the value of position exceeds the length of the file, extend the file to the length required by position.

append (1060+)

boolean

false

No

Indicates whether to enable the append mode.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.writeArrayBuffer({
    uri: 'internal://cache/test/test1.txt',
    buffer: uint8array,
    position: 0,
    success: function() {
        console.log('success ');
    },
    fail: function(errmsg, errcode) {
        console.log('failed ' + errmsg+' errcode = '+errcode);
    }
})

[h2]file.readArrayBuffer(OBJECT) (1010+)

Description

Reads data from the buffer.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

URI of a local file.

For details about the URI, please refer to File Structure > URI.

position

number

0

No

Position from which the content is read.

length

number

-

No

Length of the content to be read. If this parameter is not set, all content of the file will be read.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

buffer

uint8array

File content to be read.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

301

The file does not exist.

Sample code

file.readArrayBuffer({
    uri: 'internal://cache/test/test1.txt',
    position: 0,
    length: 3,
    success: function(data) {
        console.log("data buffer  = "+data.buffer)
    },
    fail: function(errmsg, errcode) {
        console.log('failed ' + errmsg+' errcode = '+errcode);
    }
})

[h2]file.readText(OBJECT) (1010+)

Description

Reads text.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

URI of a local file.

For details about the URI, please refer to File Structure > URI.

encoding

string

UTF-8

No

Encoding format.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Parameters returned by success

Parameter

Type

Description

text

string

Text that is read.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

301

The file does not exist.

Sample code

file.readText({
    uri: 'internal://cache/test/test.txt',
    encoding: 'UTF-8',
    success: function(data) {
        console.log('success data.text = '+data.text);
    },
    fail: function(errmsg, errcode) {
        console.log('failed ' + errmsg+' errcode = '+errcode);
    }
})

[h2]file.access(OBJECT) (1060+)

Description

Checks whether a file or directory exists.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

URI of the directory.

For details about the URI, please refer to File Structure > URI.

NOTE:

The URI can be an absolute path or a relative path if it is the path of app resources.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.access({
  uri: 'internal://files/test',
  success: function () {
    console.log('handling success')
  },
  fail: function (data, code) {
    console.log(`handling fail, code `)
  }
})

[h2]file.mkdir(OBJECT) (1060+)

Description

Creates a directory.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

File URI, which cannot be the path of app resources or be of the tmp type. The URI cannot contain a parent directory path such as ../.

recursive

boolean

-

No

Indicates whether to create the directory after recursively creating its upper-level directory. The default value is false.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.mkdir({
  uri: 'internal://files/dir/',
  success: function () {
    console.log('handling success')
  },
  fail: function (data, code) {
    console.log(`handling fail`)
  }
})

[h2]file.rmdir(OBJECT) (1060+)

Description

Deletes a directory.

To delete a directory that has a subdirectory or file, set recursive to true. Otherwise, code = 300 is returned.

OBJECT

Parameter

Type

Default Value

Mandatory

Description

uri

string

-

Yes

File URI, which cannot be the path of app resources or be of the tmp type.

recursive

boolean

-

No

Indicates whether to delete files and subdirectories recursively. The default value is false.

success

function

-

No

Callback upon success.

fail

function

-

No

Callback upon failure.

complete

function

-

No

Callback upon completion.

Result codes returned by fail

Result Code

Description

202

Invalid parameter.

300

I/O error.

Sample code

file.rmdir({
  uri: 'internal://files/dir/',
  success: function () {
    console.log('handling success')
  },
  fail: function (data, code) {
    console.log(`handling fail`)
  }
})

[h2]file.copyFileToPublicDir(OBJECT) (1078+)

Description

Copies a file to the public system directory.

Users must grant you the storage permission.

Parameters

Parameter

Type

Mandatory

Description

srcPath

string

Yes

Source file path, which must be a local path.

destPath

string

Yes

Target file path, in the format external://DIRECTORY_TYPE/xxx (optional)/yyy.file name extension, for example, external://DIRECTORY_DOCUMENTS/test/test.pdf.

The options of DIRECTORY_TYPE are as follows:

  • DIRECTORY_MUSIC
  • DIRECTORY_PICTURES
  • DIRECTORY_MOVIES
  • DIRECTORY_DCIM
  • DIRECTORY_DOCUMENTS

For HarmonyOS 2.0, Android 10.0, and their later versions, the file type must be correctly configured. Otherwise, storage exceptions may occur. Types of files that can be stored vary depending on the directory type.

  • DIRECTORY_MUSIC: audio files
  • DIRECTORY_PICTURES: images
  • DIRECTORY_MOVIES : videos
  • DIRECTORY_DCIM: images

success

function

No

Callback upon success.

fail

function

No

Callback upon failure.

complete

function

No

Callback upon completion, regardless of whether the API call is successful or fails.

Result codes returned by fail

Result Code

Description

200

General error.

202

Invalid parameter.

300

I/O exception.

302

A file already exists.

303

The file name extension does not exist.

304

Invalid file path.

Sample code

file.copyFileToPublicDir({
    srcPath: '/Common/test.mp3',
    destPath: 'external://DIRECTORY_DOCUMENTS/test.mp3',
    success: function (ret) {
      console.log("copy success: " + ret);
    },
    fail: function (data, code) {          
      console.log("handling fail, code=" + code)       
    }
})

Demo

<template>
  <div class="container">

    <div class="item-container">
      <!-- download file -->
      <div class="item-content">
        <text class="txt">downloadFile: {{fileDownloadData}}</text>
      </div>
      <input type="button" class="btn" onclick="downloadFile"  value="Download File" />

      <!-- move file -->
      <div class="item-content">
        <text class="txt">moveFile: {{fileMoveData}}</text>
      </div>
      <input type="button" class="btn" onclick="moveFile" value="Move File" />

      <!-- move file to folder -->
      <div class="item-content">
        <text class="txt">moveFileToFolder: {{fileMoveToFolderData}}</text>
      </div>
      <input type="button" class="btn" onclick="moveFileToFolder" value="Move File to Folder" />

      <!-- copy file -->
      <div class="item-content">
        <text class="txt">copyFile: {{fileCopyData}}</text>
      </div>
      <input type="button" class="btn" onclick="copyFile" value="Copy File" />

      <!-- file list -->
      <div class="item-content">
        <text class="txt">getFileList: {{fileGetListData}}</text>
      </div>
      <input type="button" class="btn" onclick="getFileList"  value="Get File List" />

      <!-- get file information -->
      <div class="item-content">
        <text class="txt">getFileInfo: {{fileGetData}}</text>
      </div>
      <input type="button" class="btn" onclick="getFile"
        value="Get File Info" />

      <!-- delete file -->
      <div class="item-content">
        <text class="txt">DeleteFile: {{fileDeleteData}}</text>
      </div>
      <input type="button" class="btn" onclick="deleteFile"  value="Delete File" />

      <!-- write file -->
      <div class="item-content">
        <text class="txt">writeFile: {{fileWriteData}}</text>
      </div>
      <input type="button" class="btn" onclick="writeFile"   value="Write File" />

      <!-- read file -->
      <div class="item-content">
        <text class="txt">readFile: {{fileReadData}}</text>
      </div>
      <input type="button" class="btn" onclick="readFile" value="Read File" />
      <!-- write Buffer -->
      <div class="item-content">
        <text class="txt">writeBuffer: {{writeBufferData}}</text>
      </div>
      <input type="button" class="btn" onclick="writeBuffer"  value="Write Buffer" />

      <!-- read Buffer -->
      <div class="item-content">
        <text class="txt">ReadBuffer: {{readBufferData}}</text>
      </div>
      <input type="button" class="btn" onclick="readBuffer"  value="Read Buffer" />
      <!-- if file is exist -->
      <div class="item-content">
        <text class="txt">fileExist: {{fileAccessData}}</text>
      </div>
      <input type="button" class="btn" onclick="access" value="File Exist" />

      <!-- make directory -->
      <div class="item-content">
        <text class="txt">makeDir: {{makeDir}}</text>
      </div>
      <input type="button" class="btn" onclick="mkDir" value="Make Dir" />

      <!-- delete directory -->
      <div class="item-content">
        <text class="txt">deletDir: {{deletDir}}</text>
      </div>
      <input type="button" class="btn" onclick="rmDir" value="Delet Dir" />
    </div>
    <!-- read text -->
      <div class="item-content">
        <text class="txt">readText: {{textContent}}</text>
      </div>
      <input type="button" class="btn" onclick="readText" value="Read Text" />
      <!-- read text -->
      <div class="item-content">
        <text class="txt">result: {{result}}</text>
      </div>
      <input type="button" class="btn" onclick="copyFileToPublicDir" value="copyFileToPublicDir" />

  </div>
</template>

<style>
.container{
  flex: 1;
  flex-direction: column;
}

  .item-container {
    margin-bottom: 50px;
    margin-right: 60px;
    margin-left: 60px;
    flex-direction: column;
  }

  .item-content {
    flex-direction: column;
    background-color: #ffffff;
    padding-left: 30px;
    padding-right: 30px;
    padding-top: 20px;
    padding-bottom: 20px;
    margin-bottom: 30px;
    align-items: flex-start;
  }

  .txt {
    padding-top: 15px;
    padding-bottom: 15px;
  }
  .btn {
  height: 80px;
  text-align: center;
  border-radius: 5px;
  margin-right: 60px;
  margin-left: 60px;
  margin-bottom: 50px;
  color: #ffffff;
  font-size: 30px;
  background-color: #0faeff;
}
</style>

<script>
  import file from '@system.file'
  import fetch from '@system.fetch'

  export default {
    data: {
      componentName: 'file',
      fileDownloadData: '',
      fileMoveData: '',
      fileMoveToFolderData: '',
      fileCopyData: '',
      fileGetListData: '',
      fileGetData: '',
      fileDeleteData: '',
      fileWriteData: '',
      fileReadData: '',
      writeBufferData: '',
      readBufferData: '',
      fileAccessData: '',
      makeDir: '',
      deletDir: '',
      textContent:'',
      result:''
    },
    onInit: function () {
      this.$page.setTitleBar({ text: 'file' })
    },
    downloadFile: function () {
      var that = this
      fetch.fetch({
        url: 'https://developer.huawei.com/dev_index/img/logo_ch.png',
        success: function (ret) {
          that.fileDownloadData = ret.data
          console.log("wlj"+ret.data);
          that.fileDeleteData = that.fileDownloadData
          console.log('file_downlaod_data--------' + JSON.stringify(ret))
        }
      })
    },
    moveFile: function () {
      var that = this
      var dstUri = 'internal://files/testfile/file.png'
      file.move({
        srcUri: this.fileDownloadData,
        dstUri: dstUri,
        success: function (ret) {
          that.fileMoveData = dstUri
          that.fileDownloadData = dstUri
          that.fileDeleteData = that.fileDownloadData
          console.log('file_save--------success ' + JSON.stringify(ret))
        },
        fail: function (errmsg, errcode) {
          that.fileMoveData = errcode + '---' + errmsg
          console.log('file_save--------fail ' + errcode + ': ' + errmsg)
        }
      })
    },
    moveFileToFolder: function () {
      var that = this
      var dstUri = 'internal://files/testfile/'
      file.move({
        srcUri: this.fileDownloadData,
        dstUri: dstUri,
        success: function (ret) {
          that.fileMoveToFolderData = ret
          that.fileDownloadData = ret
          that.fileDeleteData = ret
          console.log('file_save--------success ' + JSON.stringify(ret))
        },
        fail: function (errmsg, errcode) {
          that.fileMoveToFolderData = errcode + '---' + errmsg
          console.log('file_save--------fail ' + errcode + ': ' + errmsg)
        }
      })
    },
    copyFile: function () {
      var that = this
      var dstUri = 'internal://files/testfile/filecopy.png'
      file.copy({
        srcUri: that.fileDownloadData,
        dstUri: dstUri,
        success: function (ret) {
          that.fileCopyData = ret
          console.log('file_list_data-------- success ' + JSON.stringify(ret))
        },
        fail: function (errmsg, errcode) {
          that.fileCopyData = errcode + '---' + errmsg
          console.log('file_list_data--------complete ' + errcode + ': ' + errmsg)
        }
      })
    },
    getFileList: function () {
      var that = this
      file.list({
        uri: 'internal://files/testfile',
        success: function (ret) {
          that.fileGetListData = JSON.stringify(ret)
          console.log('file_get_data---------success ' + JSON.stringify(ret))
        },
        fail: function (errmsg, errcode) {
          that.fileGetListData = errcode + '---' + errmsg
          console.log('file_get_data---------fail ' + errcode + ': ' + errmsg)
        }
      })
    },
    getFile: function () {
      var that = this
      file.get({
        uri: 'internal://files/testfile',
        recursive: true,
        success: function (ret) {
          that.fileGetData = JSON.stringify(ret)
          console.log('file_get_data---------success ' + JSON.stringify(ret))
        },
        fail: function (errmsg, errcode) {
          that.fileGetData = errcode + '---' + errmsg
          console.log('file_get_data---------fail ' + errcode + ': ' + errmsg)
        }
      })
    },
    deleteFile: function () {
      var that = this
      file.delete({
        uri: that.fileDownloadData,
        success: function (ret) {
          that.fileDownloadData = ''
          that.fileMoveData = ''
          that.fileMoveToFolderData = ''
          that.fileCopyData = ''
          that.fileGetListData = ''
          that.fileGetData = ''
          that.fileDeleteData = JSON.stringify(ret)
          console.log('file_remove_data---------success ' + JSON.stringify(ret))
        },
        fail: function (errmsg, errcode) {
          that.fileDeleteData = errcode + '---' + errmsg
          console.log('file_remove_data---------fail ' + errcode + ': ' + errmsg)
        }
      })
    },

    writeFile: function () {
      var that = this
      file.writeText({
        uri: 'internal://cache/test/test.txt',
        text: 'hello quickapp',
        encoding: 'UTF-8',
        append: true,
        success: function () {
          console.log('file_write_data---------success ')
          that.fileWriteData = 'Success'
        },
        fail: function (errmsg, errcode) {
          that.fileWriteData = 'Fail'
          console.log('file_write_data---------failed ' + errmsg + ' errcode = ' + errcode)
        }
      })
    },

    readFile: function () {
      var that = this
      file.readText({
        uri: 'internal://cache/test/test.txt',
        encoding: 'UTF-8',
        success: function (data) {
          console.log(`success data.text = ${data.text}`)
          that.fileReadData = data.text
        },
        fail: function (errmsg, errcode) {
          console.log('failed ' + errmsg + ' errcode = ' + errcode)
          that.fileReadData = 'Failed'
        }
      })
    },
    writeBuffer: function () {
      var that = this
      var uint8 = new Uint8Array(3)
      uint8[0] = 198
      uint8[1] = 31
      uint8[2] = 219
      file.writeArrayBuffer({
        uri: 'internal://cache/test/test1.txt',
        buffer: uint8,
        position: 0,
        append: true,
        success: function () {
          console.log('success ')
          that.writeBufferData = 'Success'
        },
        fail: function (errmsg, errcode) {
          console.log('failed ' + errmsg + ' errcode = ' + errcode)
          that.writeBufferData = 'Failed'
        }
      })
    },

    readBuffer: function () {
      var that = this
      file.readArrayBuffer({
        uri: 'internal://cache/test/test1.txt',
        success: function (data) {
          that.readBufferData = data.buffer
          console.log('data buffer  = ' + data.buffer)
        },
        fail: function (errmsg, errcode) {
          console.log('failed ' + errmsg + ' errcode = ' + errcode)
        }
      })
    },

    access: function () {
      var that = this
      file.access({
        uri: 'internal://cache/test/test1.txt',
        success: function (data) {
          that.fileAccessData = 'accessable'
          console.log('success')
        },
        fail: function (errmsg, errcode) {
          that.fileAccessData = 'can not access'
          console.log('failed ' + errmsg + ' errcode = ' + errcode)
        }
      })
    },

    mkDir: function () {
      var that = this
      file.mkdir({
        uri: 'internal://cache/../test/',
        recursive: false,
        success: function (data) {
          that.makeDir = 'success'
          console.log('success')
        },
        fail: function (errmsg, errcode) {
          that.makeDir = 'fail'
          console.log('failed ' + errmsg + ' errcode = ' + errcode)
        }
      })
    },

    rmDir: function () {
      var that = this
      file.rmdir({
        uri: 'internal://cache/test/',
        recursive: true,
        success: function (data) {
          console.log('success')
          that.deletDir = 'success'
        },
        fail: function (errmsg, errcode) {
          that.deletDir = 'fail'
          console.log('failed ' + errmsg + ' errcode = ' + errcode)
        }
      })
    },
    readText() {
      var that = this
      file.readText({
        uri: 'internal://cache/test/test.txt',
        encoding: 'UTF-8',
        success: function(data) {
           console.log('success data.text = '+data.text);
           that.textContent = data.text
        },
        fail: function(errmsg, errcode) {
           console.log('failed ' + errmsg+' errcode = '+errcode);
           that.textContent = errcode
        }
      })
    },
    copyFileToPublicDir() {
      var that = this
      file.copyFileToPublicDir({
        srcPath: '/Common/logo.png',
        destPath: 'external://DIRECTORY_DOCUMENTS/test.mp3',
        success: function (ret) {
           console.log("copy success: " + ret);
           that.result = ret
      },
       fail: function (data, code) {          
           console.log("handling fail, code=" + code)
           that.result = 'fail'       
      }
    })
    }
  }
</script>

The following figure shows the final effect.

Version Change History

Version

Date

Description

1090

2022-05-19

Added result codes 303 and 304 for the copyFileToPublicDir API.

1080

2021-12-28

Added methods for file.list, file.get and file.access to access the resource folder.

1060

2019-12-10

  • Added the recursive parameter to file.get, and added type and subFile to the return information.
  • Added the append parameter to writeText and writeArrayBuffer.
  • Added the access, mkdir, and rmdir methods.

1040

2019-06-24

Added the support for the tmp type for uri in file.get.

1010

2018-04-20

Added the following APIs:

  • file.writeText: writes text into a specified file.
  • file.writeArrayBuffer: writes data into the buffer.
  • file.readArrayBuffer: reads data from the buffer.
  • file.readText: reads text.

Links

[h2]FAQs

Is the storage mechanism of the storage API the same as that of the file API?