1# Uploading and Downloading Application Files 2 3This 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. 4 5## Uploading an Application File 6 7You can use **uploadFile()** in [ohos.request](../reference/apis/js-apis-request.md) to upload local files. The system service proxy implements the upload. 8 9> **NOTE** 10> 11> Currently, only the files in the **cache/** directory (specified by **cacheDir**) can be uploaded. 12> 13> The **ohos.permission.INTERNET** permission is required for using **ohos.request**. For details about how to apply for the permission, see [Applying for Permissions](../security/accesstoken-guidelines.md). 14 15The following example demonstrates how to upload a file in the **cache** directory of an application to a network server. 16 17```ts 18// pages/xxx.ets 19import common from '@ohos.app.ability.common'; 20import fs from '@ohos.file.fs'; 21import request from '@ohos.request'; 22import { BusinessError } from '@ohos.base'; 23 24// Obtain the application file path. 25let context = getContext(this) as common.UIAbilityContext; 26let cacheDir = context.cacheDir; 27 28// Create an application file locally. 29let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 30fs.writeSync(file.fd, 'upload file test'); 31fs.closeSync(file); 32 33// Configure the upload task. 34let header = new Map<Object, string>(); 35header.set('key1', 'value1'); 36header.set('key2', 'value2'); 37let files: Array<request.File> = [ 38 { filename: 'test.txt', name: 'test', uri: 'internal://cache/test.txt', type: 'txt' } 39] 40let data: Array<request.RequestData> = [{ name: 'name', value: 'value' }]; 41let uploadConfig: request.UploadConfig = { 42 url: 'https://xxx', 43 header: header, 44 method: 'POST', 45 files: files, 46 data: data 47} 48 49// Upload the created application file to the network server. 50try { 51 request.uploadFile(context, uploadConfig) 52 .then((uploadTask: request.UploadTask) => { 53 uploadTask.on('complete', (taskStates: Array<request.TaskState>) => { 54 for (let i = 0; i < taskStates.length; i++) { 55 console.info(`upload complete taskState: ${JSON.stringify(taskStates[i])}`); 56 } 57 }); 58 }) 59 .catch((err: BusinessError) => { 60 console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); 61 }) 62} catch (error) { 63 let err: BusinessError = error as BusinessError; 64 console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); 65} 66``` 67 68## Downloading a Network Resource File to the Application File Directory 69 70You can use **downloadFile()** in [ohos.request](../reference/apis/js-apis-request.md) to download network resource files to a local application file directory. You can use the [ohos.file.fs](../reference/apis/js-apis-file-fs.md) APIs to access the downloaded files. For details, see [Accessing Application Files](app-file-access.md). The system service proxy implements the download. 71 72> **NOTE** 73> 74> Currently, network resource files can be downloaded only to the application file directory. 75> 76> The **ohos.permission.INTERNET** permission is required for using **ohos.request**. For details about how to apply for the permission, see [Applying for Permissions](../security/accesstoken-guidelines.md). 77 78The following example demonstrates how to download a network resource file to a local application file directory. 79 80```ts 81// pages/xxx.ets 82// Download the network resource file to the local application file directory, and read data from the file. 83import common from '@ohos.app.ability.common'; 84import fs from '@ohos.file.fs'; 85import request from '@ohos.request'; 86import { BusinessError } from '@ohos.base'; 87import buffer from '@ohos.buffer'; 88 89// Obtain the application file path. 90let context = getContext(this) as common.UIAbilityContext; 91let filesDir = context.filesDir; 92 93try { 94 request.downloadFile(context, { 95 url: 'https://xxxx/xxxx.txt', 96 filePath: filesDir + '/xxxx.txt' 97 }).then((downloadTask: request.DownloadTask) => { 98 downloadTask.on('complete', () => { 99 console.info('download complete'); 100 let file = fs.openSync(filesDir + '/xxxx.txt', fs.OpenMode.READ_WRITE); 101 let arrayBuffer = new ArrayBuffer(1024); 102 let readLen = fs.readSync(file.fd, arrayBuffer); 103 let buf = buffer.from(arrayBuffer, 0, readLen); 104 console.info(`The content of file: ${buf.toString()}`); 105 fs.closeSync(file); 106 }) 107 }).catch((err: BusinessError) => { 108 console.error(`Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`); 109 }); 110} catch (error) { 111 let err: BusinessError = error as BusinessError; 112 console.error(`Invoke downloadFile failed, code is ${err.code}, message is ${err.message}`); 113} 114``` 115