1e41f4b71Sopenharmony_ci# Uploading and Downloading Application Files 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThis topic describes how to upload an application file to a network server and download a network resource file from a network server to a local application file directory. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## Uploading an Application File 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciYou can use **uploadFile()** in [ohos.request](../../reference/apis-basic-services-kit/js-apis-request.md) to upload local files. The system service agent uploads the files. In API version 12, you can set the address of the agent in **request.agent.create()**. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci> **NOTE** 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> Currently, only the files in the **cache/** directory (specified by **cacheDir**) can be uploaded. 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> To use **uploadFile()** in **ohos.request**, you need to [declare permissions](../../security/AccessToken/declare-permissions.md): ohos.permission.INTERNET. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciThe following code demonstrates how to upload files from a cache directory of an application to a network server in two ways: 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```ts 18e41f4b71Sopenharmony_ci// Way 1: Use request.uploadFile. 19e41f4b71Sopenharmony_ci// pages/xxx.ets 20e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 21e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 22e41f4b71Sopenharmony_ciimport { BusinessError, request } from '@kit.BasicServicesKit'; 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci// Obtain the application file path. 25e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext; 26e41f4b71Sopenharmony_cilet cacheDir = context.cacheDir; 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci// Create an application file locally. 29e41f4b71Sopenharmony_cilet file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 30e41f4b71Sopenharmony_cifs.writeSync(file.fd, 'upload file test'); 31e41f4b71Sopenharmony_cifs.closeSync(file); 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci// Configure the upload task. 34e41f4b71Sopenharmony_cilet header = new Map<Object, string>(); 35e41f4b71Sopenharmony_ciheader.set('key1', 'value1'); 36e41f4b71Sopenharmony_ciheader.set('key2', 'value2'); 37e41f4b71Sopenharmony_cilet files: Array<request.File> = [ 38e41f4b71Sopenharmony_ci// "internal://cache" in uri corresponds to the cacheDir directory. 39e41f4b71Sopenharmony_ci { filename: 'test.txt', name: 'test', uri: 'internal://cache/test.txt', type: 'txt' } 40e41f4b71Sopenharmony_ci] 41e41f4b71Sopenharmony_cilet data: Array<request.RequestData> = [{ name: 'name', value: 'value' }]; 42e41f4b71Sopenharmony_cilet uploadConfig: request.UploadConfig = { 43e41f4b71Sopenharmony_ci url: 'https://xxx', 44e41f4b71Sopenharmony_ci header: header, 45e41f4b71Sopenharmony_ci method: 'POST', 46e41f4b71Sopenharmony_ci files: files, 47e41f4b71Sopenharmony_ci data: data 48e41f4b71Sopenharmony_ci} 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci// Upload the created application file to the network server. 51e41f4b71Sopenharmony_citry { 52e41f4b71Sopenharmony_ci request.uploadFile(context, uploadConfig) 53e41f4b71Sopenharmony_ci .then((uploadTask: request.UploadTask) => { 54e41f4b71Sopenharmony_ci uploadTask.on('complete', (taskStates: Array<request.TaskState>) => { 55e41f4b71Sopenharmony_ci for (let i = 0; i < taskStates.length; i++) { 56e41f4b71Sopenharmony_ci console.info(`upload complete taskState: ${JSON.stringify(taskStates[i])}`); 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci }); 59e41f4b71Sopenharmony_ci }) 60e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 61e41f4b71Sopenharmony_ci console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); 62e41f4b71Sopenharmony_ci }) 63e41f4b71Sopenharmony_ci} catch (error) { 64e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 65e41f4b71Sopenharmony_ci console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_ci``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci```ts 70e41f4b71Sopenharmony_ci// Way 2: Use request.agent. 71e41f4b71Sopenharmony_ci// pages/xxx.ets 72e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 73e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 74e41f4b71Sopenharmony_ciimport { BusinessError, request } from '@kit.BasicServicesKit'; 75e41f4b71Sopenharmony_ci// Obtain the application file path. 76e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext; 77e41f4b71Sopenharmony_cilet cacheDir = context.cacheDir; 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci// Create an application file locally. 80e41f4b71Sopenharmony_cilet file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 81e41f4b71Sopenharmony_cifs.writeSync(file.fd, 'upload file test'); 82e41f4b71Sopenharmony_cifs.closeSync(file); 83e41f4b71Sopenharmony_cilet attachments: Array<request.agent.FormItem> = [{ 84e41f4b71Sopenharmony_ci name: "test", 85e41f4b71Sopenharmony_ci value: [ 86e41f4b71Sopenharmony_ci { 87e41f4b71Sopenharmony_ci filename: "test.txt", 88e41f4b71Sopenharmony_ci path: "./test.txt", 89e41f4b71Sopenharmony_ci }, 90e41f4b71Sopenharmony_ci ] 91e41f4b71Sopenharmony_ci}]; 92e41f4b71Sopenharmony_cilet config: request.agent.Config = { 93e41f4b71Sopenharmony_ci action: request.agent.Action.UPLOAD, 94e41f4b71Sopenharmony_ci url: 'http://xxx', 95e41f4b71Sopenharmony_ci mode: request.agent.Mode.FOREGROUND, 96e41f4b71Sopenharmony_ci overwrite: true, 97e41f4b71Sopenharmony_ci method: "POST", 98e41f4b71Sopenharmony_ci data: attachments, 99e41f4b71Sopenharmony_ci saveas: "./" 100e41f4b71Sopenharmony_ci}; 101e41f4b71Sopenharmony_cirequest.agent.create(getContext(), config).then((task: request.agent.Task) => { 102e41f4b71Sopenharmony_ci task.start((err: BusinessError) => { 103e41f4b71Sopenharmony_ci if (err) { 104e41f4b71Sopenharmony_ci console.error(`Failed to start the upload task, Code: ${err.code} message: ${err.message}`); 105e41f4b71Sopenharmony_ci return; 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci }); 108e41f4b71Sopenharmony_ci task.on('progress', async(progress) => { 109e41f4b71Sopenharmony_ci console.warn(`/Request upload status ${progress.state}, uploaded ${progress.processed}`); 110e41f4b71Sopenharmony_ci }) 111e41f4b71Sopenharmony_ci task.on('completed', async() => { 112e41f4b71Sopenharmony_ci console.warn(`/Request upload completed`); 113e41f4b71Sopenharmony_ci }) 114e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 115e41f4b71Sopenharmony_ci console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`); 116e41f4b71Sopenharmony_ci}); 117e41f4b71Sopenharmony_ci``` 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci## Downloading a Network Resource File to an Application Directory 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ciYou can use **downloadFile()** in [ohos.request](../../reference/apis-basic-services-kit/js-apis-request.md) to download network resource files to a local application directory. You can use the [ohos.file.fs](../../reference/apis-core-file-kit/js-apis-file-fs.md) APIs to access the downloaded files. For details, see [Accessing Application Files](../../file-management/app-file-access.md). The system service agent downloads the files. In API version 12, you can set the address of the agent in **request.agent.create()**. 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci> **NOTE** 124e41f4b71Sopenharmony_ci> 125e41f4b71Sopenharmony_ci> Currently, network resource files can be downloaded only to the application file directory. 126e41f4b71Sopenharmony_ci> 127e41f4b71Sopenharmony_ci> To use **uploadFile()** in **ohos.request**, you need to [declare permissions](../../security/AccessToken/declare-permissions.md): ohos.permission.INTERNET. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciThe following code demonstrates how to download files from a network server to an application directory in two ways: 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci```ts 132e41f4b71Sopenharmony_ci// Way 1: Use request.downloadFile. 133e41f4b71Sopenharmony_ci// pages/xxx.ets 134e41f4b71Sopenharmony_ci// Download the network resource file to the local application file directory, and read data from the file. 135e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 136e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 137e41f4b71Sopenharmony_ciimport { BusinessError, request } from '@kit.BasicServicesKit'; 138e41f4b71Sopenharmony_ciimport { buffer } from '@kit.ArkTS'; 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci// Obtain the application file path. 141e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext; 142e41f4b71Sopenharmony_cilet filesDir = context.filesDir; 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_citry { 145e41f4b71Sopenharmony_ci request.downloadFile(context, { 146e41f4b71Sopenharmony_ci url: 'https://xxxx/xxxx.txt', 147e41f4b71Sopenharmony_ci filePath: filesDir + '/xxxx.txt' 148e41f4b71Sopenharmony_ci }).then((downloadTask: request.DownloadTask) => { 149e41f4b71Sopenharmony_ci downloadTask.on('complete', () => { 150e41f4b71Sopenharmony_ci console.info('download complete'); 151e41f4b71Sopenharmony_ci let file = fs.openSync(filesDir + '/xxxx.txt', fs.OpenMode.READ_WRITE); 152e41f4b71Sopenharmony_ci let arrayBuffer = new ArrayBuffer(1024); 153e41f4b71Sopenharmony_ci let readLen = fs.readSync(file.fd, arrayBuffer); 154e41f4b71Sopenharmony_ci let buf = buffer.from(arrayBuffer, 0, readLen); 155e41f4b71Sopenharmony_ci console.info(`The content of file: ${buf.toString()}`); 156e41f4b71Sopenharmony_ci fs.closeSync(file); 157e41f4b71Sopenharmony_ci }) 158e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 159e41f4b71Sopenharmony_ci console.error(`Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`); 160e41f4b71Sopenharmony_ci }); 161e41f4b71Sopenharmony_ci} catch (error) { 162e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 163e41f4b71Sopenharmony_ci console.error(`Invoke downloadFile failed, code is ${err.code}, message is ${err.message}`); 164e41f4b71Sopenharmony_ci} 165e41f4b71Sopenharmony_ci``` 166e41f4b71Sopenharmony_ci```ts 167e41f4b71Sopenharmony_ci// Way 2: Use request.agent. 168e41f4b71Sopenharmony_ci// pages/xxx.ets 169e41f4b71Sopenharmony_ci// Download the network resource file to the local application file directory, and read data from the file. 170e41f4b71Sopenharmony_ciimport { BusinessError, request } from '@kit.BasicServicesKit'; 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_cilet config: request.agent.Config = { 173e41f4b71Sopenharmony_ci action: request.agent.Action.DOWNLOAD, 174e41f4b71Sopenharmony_ci url: 'https://xxxx/test.txt', 175e41f4b71Sopenharmony_ci gauge: true, 176e41f4b71Sopenharmony_ci overwrite: true, 177e41f4b71Sopenharmony_ci network: request.agent.Network.WIFI, 178e41f4b71Sopenharmony_ci}; 179e41f4b71Sopenharmony_cirequest.agent.create(getContext(), config).then((task: request.agent.Task) => { 180e41f4b71Sopenharmony_ci task.start((err: BusinessError) => { 181e41f4b71Sopenharmony_ci if (err) { 182e41f4b71Sopenharmony_ci console.error(`Failed to start the upload task, Code: ${err.code} message: ${err.message}`); 183e41f4b71Sopenharmony_ci return; 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci }); 186e41f4b71Sopenharmony_ci task.on('progress', async(progress) => { 187e41f4b71Sopenharmony_ci console.warn(`/Request download status ${progress.state}, downloaded ${progress.processed}`); 188e41f4b71Sopenharmony_ci }) 189e41f4b71Sopenharmony_ci task.on('completed', async() => { 190e41f4b71Sopenharmony_ci console.warn(`/Request download completed`); 191e41f4b71Sopenharmony_ci }) 192e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 193e41f4b71Sopenharmony_ci console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`); 194e41f4b71Sopenharmony_ci}); 195e41f4b71Sopenharmony_ci``` 196