1# Saving User Files 2 3When 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. 4 5The operations for saving images, audio or video clips, and documents are similar. Call **save()** of the corresponding picker instance and pass in **saveOptions**. 6 7 8## Saving Images or Video Files 9 101. Import the **picker** module and **fs** module. 11 12 ```ts 13 import picker from '@ohos.file.picker'; 14 import fs from '@ohos.file.fs'; 15 ``` 16 172. Create a **photoSaveOptions** instance. 18 19 ```ts 20 const photoSaveOptions = new picker.PhotoSaveOptions(); // Create a photoSaveOptions instance. 21 photoSaveOptions.newFileNames = ["PhotoViewPicker01.jpg"]; // (Optional) Set the names of the files to save. 22 ``` 23 243. 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. 25 26 <br>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. 27 28 ```ts 29 let URI = null; 30 const photoViewPicker = new picker.PhotoViewPicker(); 31 photoViewPicker.save(photoSaveOptions).then((photoSaveResult) => { 32 URI = photoSaveResult[0]; 33 console.info('photoViewPicker.save to file succeed and URI is:' + URI); 34 }).catch((err) => { 35 console.error(`Invoke photoViewPicker.save failed, code is ${err.code}, message is ${err.message}`); 36 }) 37 ``` 38 394. 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**. 40 41 ```ts 42 let file = fs.openSync(URI, fs.OpenMode.READ_WRITE); 43 console.info('file fd: ' + file.fd); 44 ``` 45 465. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD. 47 48 ```ts 49 let writeLen = fs.writeSync(file.fd, 'hello, world'); 50 console.info('write data to file succeed and size is:' + writeLen); 51 fs.closeSync(file); 52 ``` 53 54## Saving Documents 55 561. Import the **picker** module and **fs** module. 57 58 ```ts 59 import picker from '@ohos.file.picker'; 60 import fs from '@ohos.file.fs'; 61 ``` 62 632. Create a **documentSaveOptions** instance. 64 65 ```ts 66 const documentSaveOptions = new picker.DocumentSaveOptions(); // Create a documentSaveOptions instance. 67 documentSaveOptions.newFileNames = ["DocumentViewPicker01.txt"]; // (Optional) Set the names of the documents to save. 68 ``` 69 703. 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. 71 72 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. 73 74 ```ts 75 let URI = null; 76 const documentViewPicker = new picker.DocumentViewPicker(); // Create a documentViewPicker instance. 77 documentViewPicker.save(documentSaveOptions).then((documentSaveResult) => { 78 URI = documentSaveResult[0]; 79 console.info('documentViewPicker.save to file succeed and URI is:' + URI); 80 }).catch((err) => { 81 console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`); 82 }) 83 ``` 84 854. 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**. 86 87 ```ts 88 let file = fs.openSync(URI, fs.OpenMode.READ_WRITE); 89 console.info('file fd: ' + file.fd); 90 ``` 91 925. Use [fs.writeSync()](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD. 93 94 ```ts 95 let writeLen = fs.writeSync(file.fd, 'hello, world'); 96 console.info('write data to file succeed and size is:' + writeLen); 97 fs.closeSync(file); 98 ``` 99 100## Saving Audio Files 101 1021. Import the **picker** module and **fs** module. 103 104 ```ts 105 import picker from '@ohos.file.picker'; 106 import fs from '@ohos.file.fs'; 107 ``` 108 1092. Create an **audioSaveOptions** instance. 110 111 ```ts 112 const audioSaveOptions = new picker.AudioSaveOptions(); // Create an audioSaveOptions instance. 113 audioSaveOptions.newFileNames = ['AudioViewPicker01.mp3']; // (Optional) Set the names of the files to save. 114 ``` 115 1163. 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. 117 118 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. 119 120 ```ts 121 let URI = null; 122 const audioViewPicker = new picker.AudioViewPicker(); 123 audioViewPicker.save(audioSaveOptions).then((audioSelectResult) => { 124 URI = audioSelectResult[0]; 125 console.info('audioViewPicker.save to file succeed and URI is:' + URI); 126 }).catch((err) => { 127 console.error(`Invoke audioViewPicker.save failed, code is ${err.code}, message is ${err.message}`); 128 }) 129 ``` 130 1314. 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**. 132 133 ```ts 134 let file = fs.openSync(URI, fs.OpenMode.READ_WRITE); 135 console.info('file fd: ' + file.fd); 136 ``` 137 1385. Use [fs.writeSync](../reference/apis/js-apis-file-fs.md#writesync) to edit the file based on the FD, and then close the FD. 139 140 ```ts 141 let writeLen = fs.writeSync(file.fd, 'hello, world'); 142 console.info('write data to file succeed and size is:' + writeLen); 143 fs.closeSync(file); 144 ``` 145 146