harmony 鸿蒙Uploading and Downloading Application Files
Uploading and Downloading Application Files
This 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.
Uploading an Application File
You can use uploadFile() in ohos.request 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().
NOTE
Currently, only the files in the cache/ directory (specified by cacheDir) can be uploaded.
To use uploadFile() in ohos.request, you need to declare permissions: ohos.permission.INTERNET.
The following code demonstrates how to upload files from a cache directory of an application to a network server in two ways:
// Way 1: Use request.uploadFile.
// pages/xxx.ets
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { BusinessError, request } from '@kit.BasicServicesKit';
// Obtain the application file path.
let context = getContext(this) as common.UIAbilityContext;
let cacheDir = context.cacheDir;
// Create an application file locally.
let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);
fs.writeSync(file.fd, 'upload file test');
fs.closeSync(file);
// Configure the upload task.
let files: Array<request.File> = [
// "internal://cache" in uri corresponds to the cacheDir directory.
{ filename: 'test.txt', name: 'test', uri: 'internal://cache/test.txt', type: 'txt' }
]
let data: Array<request.RequestData> = [{ name: 'name', value: 'value' }];
let uploadConfig: request.UploadConfig = {
url: 'https://xxx',
header: {
'key1':'value1',
'key2':'value2'
},
method: 'POST',
files: files,
data: data
}
// Upload the created application file to the network server.
try {
request.uploadFile(context, uploadConfig)
.then((uploadTask: request.UploadTask) => {
uploadTask.on('complete', (taskStates: Array<request.TaskState>) => {
for (let i = 0; i < taskStates.length; i++) {
console.info(`upload complete taskState: ${JSON.stringify(taskStates[i])}`);
}
});
})
.catch((err: BusinessError) => {
console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
})
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
}
// Way 2: Use request.agent.
// pages/xxx.ets
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { BusinessError, request } from '@kit.BasicServicesKit';
// Obtain the application file path.
let context = getContext(this) as common.UIAbilityContext;
let cacheDir = context.cacheDir;
// Create an application file locally.
let file = fs.openSync(cacheDir + '/test.txt', fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);
fs.writeSync(file.fd, 'upload file test');
fs.closeSync(file);
let attachments: Array<request.agent.FormItem> = [{
name: "test",
value: [
{
filename: "test.txt",
path: "./test.txt",
},
]
}];
let config: request.agent.Config = {
action: request.agent.Action.UPLOAD,
url: 'http://xxx',
mode: request.agent.Mode.FOREGROUND,
overwrite: true,
method: "POST",
headers: {
'key1':'value1',
'key2':'value2'
},
data: attachments,
saveas: "./"
};
request.agent.create(getContext(), config).then((task: request.agent.Task) => {
task.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the upload task, Code: ${err.code} message: ${err.message}`);
return;
}
});
task.on('progress', async(progress) => {
console.warn(`/Request upload status ${progress.state}, uploaded ${progress.processed}`);
})
task.on('completed', async() => {
console.warn(`/Request upload completed`);
// This method requires the user to manage the task lifecycle. After the task is complete, call the remove method to release the task object.
request.agent.remove(task.tid);
})
}).catch((err: BusinessError) => {
console.error(`Failed to create a upload task, Code: ${err.code}, message: ${err.message}`);
});
Downloading a Network Resource File to an Application Directory
You can use downloadFile() in ohos.request to download network resource files to a local application directory. You can use the ohos.file.fs APIs to access the downloaded files. For details, see Accessing Application Files. The system service agent downloads the files. In API version 12, you can set the address of the agent in request.agent.create().
NOTE
Currently, network resource files can be downloaded only to the application file directory.
To use uploadFile() in ohos.request, you need to declare permissions: ohos.permission.INTERNET.
The following code demonstrates how to download files from a network server to an application directory in two ways:
// Way 1: Use request.downloadFile.
// pages/xxx.ets
// Download the network resource file to the local application file directory, and read data from the file.
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { BusinessError, request } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
// Obtain the application file path.
let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
try {
request.downloadFile(context, {
url: 'https://xxxx/xxxx.txt',
filePath: filesDir + '/xxxx.txt'
}).then((downloadTask: request.DownloadTask) => {
downloadTask.on('complete', () => {
console.info('download complete');
let file = fs.openSync(filesDir + '/xxxx.txt', fs.OpenMode.READ_WRITE);
let arrayBuffer = new ArrayBuffer(1024);
let readLen = fs.readSync(file.fd, arrayBuffer);
let buf = buffer.from(arrayBuffer, 0, readLen);
console.info(`The content of file: ${buf.toString()}`);
fs.closeSync(file);
})
}).catch((err: BusinessError) => {
console.error(`Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`Invoke downloadFile failed, code is ${err.code}, message is ${err.message}`);
}
// Way 2: Use request.agent.
// pages/xxx.ets
// Download the network resource file to the local application file directory, and read data from the file.
import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { BusinessError, request } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
// Obtain the application file path.
let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
let config: request.agent.Config = {
action: request.agent.Action.DOWNLOAD,
url: 'https://xxxx/test.txt',
saveas: 'xxxx.txt',
gauge: true,
overwrite: true,
network: request.agent.Network.WIFI,
};
request.agent.create(context, config).then((task: request.agent.Task) => {
task.start((err: BusinessError) => {
if (err) {
console.error(`Failed to start the download task, Code: ${err.code} message: ${err.message}`);
return;
}
});
task.on('progress', async(progress) => {
console.warn(`/Request download status ${progress.state}, downloaded ${progress.processed}`);
})
task.on('completed', async() => {
console.warn(`/Request download completed`);
let file = fs.openSync(filesDir + '/xxxx.txt', fs.OpenMode.READ_WRITE);
let arrayBuffer = new ArrayBuffer(1024);
let readLen = fs.readSync(file.fd, arrayBuffer);
let buf = buffer.from(arrayBuffer, 0, readLen);
console.info(`The content of file: ${buf.toString()}`);
fs.closeSync(file);
// This method requires the user to manage the task lifecycle. After the task is complete, call the remove method to release the task object.
request.agent.remove(task.tid);
})
}).catch((err: BusinessError) => {
console.error(`Failed to create a download task, Code: ${err.code}, message: ${err.message}`);
});
Adding Network Configuration
Intercepting HTTP
You can set the configuration file to intercept HTTP. After HTTP is disabled for the upload and download module, upload and download tasks using plaintext HTTP cannot be created. The configuration file is stored in the src/main/resources/base/profile/network_config.json directory of the application.
The sample configuration file is as follows:
{
"network-security-config": {
"base-config": {
"cleartextTrafficPermitted": true,
"trust-anchors": [
{
"certificates": "/etc/security/certificates"
}
]
},
"domain-config": [
{
"cleartextTrafficPermitted": true,
"domains": [
{
"include-subdomains": true,
"name": "*.example.com"
}
],
}
]
}
}
The table below lists the description of each field.
|Field |Type |Description |
|————————–|—————|————————————–|
|base-config:
cleartextTrafficPermitted|boolean|Whether plaintext transfer is allowed in the global configuration. The value true means that plaintext transfer is allowed, that is, HTTP is not intercepted, and false means the opposite.|
|domain-config:
cleartextTrafficPermitted|boolean|Whether plaintext transfer is allowed in a specified domain. The value true means that plaintext transfer is allowed, that is, HTTP is not intercepted, and false means the opposite.|
|include-subdomains|boolean|Whether a rule applies to subdomains. The value true means that the regular expression is used to match the domain name, and false means the opposite.|
你可能感兴趣的鸿蒙文章
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦