save-user-file.md 6.9 KB
Newer Older
A
Annie_wang 已提交
1 2 3 4 5 6
# Saving User Files

When a user needs to download a file from the network to a local directory or save a user file into another directory, use **FilePicker** to save the file.

The operations for saving images, audio or video clips, and documents are similar. Call **save()** of the corresponding picker instance and pass in **saveOptions**.

A
Annie_wang 已提交
7 8
The **save()** interface saves the file in the file manager, not in the Gallery.

A
Annie_wang 已提交
9 10 11

## Saving Images or Video Files

A
Annie_wang 已提交
12
1. Import the **picker** module and **fs** module.
A
Annie_wang 已提交
13 14 15

   ```ts
   import picker from '@ohos.file.picker';
A
Annie_wang 已提交
16
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
17 18 19 20 21 22 23 24 25
   ```

2. Create a **photoSaveOptions** instance.

   ```ts
   const photoSaveOptions = new picker.PhotoSaveOptions(); // Create a photoSaveOptions instance.
   photoSaveOptions.newFileNames = ["PhotoViewPicker01.jpg"]; // (Optional) Set the names of the files to save.
   ```

A
Annie_wang 已提交
26 27
3. Create a **photoViewPicker** instance and call [save()](../reference/apis/js-apis-file-picker.md#save) to open the **FilePicker** page to save the files. After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
   
A
Annie_wang 已提交
28
   The permission on the URIs returned by **save()** is read/write. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
A
Annie_wang 已提交
29 30

   ```ts
A
Annie_wang 已提交
31
   let uri = null;
A
Annie_wang 已提交
32
   const photoViewPicker = new picker.PhotoViewPicker();
A
Annie_wang 已提交
33
   photoViewPicker.save(photoSaveOptions).then((photoSaveResult) => {
A
Annie_wang 已提交
34 35
     uri = photoSaveResult[0];
     console.info('photoViewPicker.save to file succeed and uri is:' + uri);
A
Annie_wang 已提交
36 37 38 39 40 41 42 43
   }).catch((err) => {
     console.error(`Invoke photoViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
   })
   ```

4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.

   ```ts
A
Annie_wang 已提交
44
   let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
A
Annie_wang 已提交
45 46 47 48 49 50 51 52 53
   console.info('file fd: ' + file.fd);
   ```

5. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD.

   ```ts
   let writeLen = fs.writeSync(file.fd, 'hello, world');
   console.info('write data to file succeed and size is:' + writeLen);
   fs.closeSync(file);
A
Annie_wang 已提交
54 55 56 57
   ```

## Saving Documents

A
Annie_wang 已提交
58
1. Import the **picker** module and **fs** module.
A
Annie_wang 已提交
59 60 61

   ```ts
   import picker from '@ohos.file.picker';
A
Annie_wang 已提交
62
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
63 64 65 66 67 68 69 70 71
   ```

2. Create a **documentSaveOptions** instance.

   ```ts
   const documentSaveOptions = new picker.DocumentSaveOptions(); // Create a documentSaveOptions instance.
   documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; // (Optional) Set the names of the documents to save.
   ```

A
Annie_wang 已提交
72 73 74
3. Create a **documentViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-3) to open the **FilePicker** page to save the documents. After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
   
   The permission on the URIs returned by **save()** is read/write. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
A
Annie_wang 已提交
75 76

   ```ts
A
Annie_wang 已提交
77
   let uri = null;
A
Annie_wang 已提交
78
   const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance.
A
Annie_wang 已提交
79
   documentViewPicker.save(documentSaveOptions).then((documentSaveResult) => {
A
Annie_wang 已提交
80 81
     uri = documentSaveResult[0];
     console.info('documentViewPicker.save to file succeed and uri is:' + uri);
A
Annie_wang 已提交
82 83 84 85 86 87 88 89
   }).catch((err) => {
     console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
   })
   ```

4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.

   ```ts
A
Annie_wang 已提交
90
   let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
A
Annie_wang 已提交
91 92 93 94 95 96 97 98 99
   console.info('file fd: ' + file.fd);
   ```

5. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD.

   ```ts
   let writeLen = fs.writeSync(file.fd, 'hello, world');
   console.info('write data to file succeed and size is:' + writeLen);
   fs.closeSync(file);
A
Annie_wang 已提交
100 101 102 103
   ```

## Saving Audio Files

A
Annie_wang 已提交
104
1. Import the **picker** module and **fs** module.
A
Annie_wang 已提交
105 106 107

   ```ts
   import picker from '@ohos.file.picker';
A
Annie_wang 已提交
108
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
109 110 111 112 113 114 115 116 117
   ```

2. Create an **audioSaveOptions** instance.

   ```ts
   const audioSaveOptions = new picker.AudioSaveOptions(); // Create an audioSaveOptions instance.
   audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3']; // (Optional) Set the names of the files to save.
   ```

A
Annie_wang 已提交
118 119 120 121
3. Create an **audioViewPicker** instance, and call [save()](../reference/apis/js-apis-file-picker.md#save-6) to open the **FilePicker** page to save the files. After the user selects the target folder, the file saving operation is complete. After the files are saved successfully, the URIs of the files saved are returned.
   
   The permission on the URIs returned by **save()** is read/write. Further file operations can be performed based on the URIs in the result set. Note that the URI cannot be directly used in the **picker** callback to open a file. You need to define a global variable to save the URI and use a button to trigger file opening.
   
A
Annie_wang 已提交
122
   ```ts
A
Annie_wang 已提交
123
   let uri = null;
A
Annie_wang 已提交
124
   const audioViewPicker = new picker.AudioViewPicker();
A
Annie_wang 已提交
125
   audioViewPicker.save(audioSaveOptions).then((audioSelectResult) => {
A
Annie_wang 已提交
126 127
     uri = audioSelectResult[0];
     console.info('audioViewPicker.save to file succeed and uri is:' + uri);
A
Annie_wang 已提交
128 129 130 131 132 133 134 135
   }).catch((err) => {
     console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
   })
   ```

4. Use a button to trigger invocation of other functions. Use [fs.openSync()](../reference/apis/js-apis-file-fs.md#fsopensync) to open the file based on the URI and obtain the FD. Note that the **mode** parameter of **fs.openSync()** must be **fs.OpenMode.READ_WRITE**.

   ```ts
A
Annie_wang 已提交
136
   let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
A
Annie_wang 已提交
137 138 139
   console.info('file fd: ' + file.fd);
   ```

A
Annie_wang 已提交
140
5. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD.
A
Annie_wang 已提交
141 142 143 144 145

   ```ts
   let writeLen = fs.writeSync(file.fd, 'hello, world');
   console.info('write data to file succeed and size is:' + writeLen);
   fs.closeSync(file);
A
Annie_wang 已提交
146
   ```
A
Annie_wang 已提交
147