• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copying Files Across Devices
2
3The distributed file system provides the cross-device file copy capability for applications. You can use the [file management APIs](../reference/apis/js-apis-file-fs.md) to copy files across devices and applications. This topic walks you through the process of copying a file from device A to device B.
4
5## How to Develop
6
71. Connect the devices to form a Super Device.<br>
8   Connect the devices to a LAN, and complete device authentication. The devices must be logged in with the same account.
9
102. Copy a file across devices. To copy a file of the same application across devices, place the file in the **distributedfiles/** directory of the application sandbox directory.
11
12   Copy the file of device A from the sandbox directory to the **distributedfiles/** directory of device A.
13
14   ```ts
15   import fs from '@ohos.file.fs';
16   import common from '@ohos.app.ability.common';
17   import { BusinessError } from '@ohos.base';
18   import fileUri from '@ohos.file.fileuri';
19
20   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device A.
21   let pathDir: string = context.filesDir;
22   let distributedPathDir: string = context.distributedFilesDir;
23   // Sandbox directory of the file to copy
24   let filePath: string = pathDir + '/src.txt';
25
26   try {
27    // If the file does not exist, create a file and write data to the file created.
28    let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
29    fs.writeSync(file.fd, 'Create file success');
30    fs.closeSync(file);
31   } catch (err: BusinessError) {
32    console.error(`Failed to createFile. Code: ${err.code}, message: ${err.message}`);
33   }
34
35   // Obtain the URI of the file to copy.
36   let fileUri = fileUri.getUriFromPath(filePath);
37
38   // Copy the file from the sandbox directory to the distributed file directory.
39   let destUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt');
40
41   try {
42    // Copy the file from the sandbox directory to the distributed file directory.
43    fs.copy(fileUri, destUri).then(()=>{
44      console.info("Succeeded in copying---. ");
45      console.info("src: " + fileUri + "dest: " + destUri);
46    }).catch((err: BusinessError)=>{
47      console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`);
48    })
49   } catch (err: BusinessError) {
50    console.error(`Failed to getData. Code: ${err.code}, message: ${err.message}`);
51   }
52   ```
53
54   Device B copies the file from the distributed file directory of device B.
55
56   ```ts
57   import fs from '@ohos.file.fs';
58   import common from '@ohos.app.ability.common';
59   import { BusinessError } from '@ohos.base';
60   import fileUri from '@ohos.file.fileuri';
61
62   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device B.
63   let pathDir: string = context.filesDir;
64   let distributedPathDir: string = context.distributedFilesDir;
65   // Destination sandbox directory to which the file is to be copied.
66   let filePath: string = pathDir + '/dest.txt';
67
68   // Obtain the URI of the destination path.
69   let destUri = fileUri.getUriFromPath(filePath);
70
71   // Obtain the file in the distributed file directory.
72   let srcUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt');
73
74   // Define a callback for the file copy operation.
75   let progressListener: fs.ProgressListener = (progress: fs.Progress) => {
76     console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
77   };
78   let options: fs.CopyOptions = {
79     "progressListener" : progressListener
80   }
81
82   try {
83    // Copy the file in the distributed file directory to the destination sandbox directory.
84    fs.copy(srcUri, destUri, options).then(()=>{
85      console.info("Succeeded in copying of paste. ");
86      console.info("src: " + fileUri + "dest: " + destUri); // file://com.example.myapplication/data/storage/el2/distributedfiles/src.txt
87    }).catch((err: BusinessError)=>{
88      console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`);
89    })
90   } catch (err: BusinessError) {
91    console.error(`Failed to copy. Code: ${err.code}, message: ${err.message}`);
92   }
93   ```
94