harmony 鸿蒙@system.request (Upload and Download)

2022-08-09 浏览 (747)

@system.request (Upload and Download)

The system.request module provides applications with basic upload and download capabilities.

NOTE

  • The APIs of this module are deprecated since API version 9. You are advised to use @ohos.request instead.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import request from '@system.request';

request.upload

upload(options: UploadRequestOptions): void

Uploads a file. This API returns no value.

System capability: SystemCapability.MiscServices.Upload

Parameters

NameTypeMandatoryDescription
optionsUploadRequestOptionsYesUpload configurations.

Example

import request, { UploadRequestOptions } from '@system.request';

let uploadRequestOptions: UploadRequestOptions = {
  url: 'http://www.path.com',
  method: 'POST',
  files: [{
    filename: "test",
    name: "test",
    uri: "internal://cache/test.jpg",
    type: "jpg"
  }],
  data: [{
    name: "name123",
    value: "123"
  }],
  success: (data: object) => {
    console.info(' upload success, code:' + JSON.stringify(data));
  },
  fail: (data:string, code:number) => {
    console.info(' upload fail data: ' + data + 'code: ' + code);
  },
  complete: () => {
    console.info(' upload complete');
  }
}

try {
  request.upload(uploadRequestOptions);
  console.info('upload start ');
} catch (err) {
  console.info(' upload err:' + err);
}

UploadRequestOptions

System capability: SystemCapability.MiscServices.Upload

NameTypeMandatoryDescription
urlstringYesURL of the upload server.
dataArray<RequestData>NoForm data in the request body.
filesArray<RequestFile>YesList of files to upload, which is submitted through multipart/form-data.
headerObjectNoRequest header.
methodstringNoRequest method, which can be 'POST' or 'PUT'. The default value is POST.
successFunctionNoCalled when API call is successful.
failFunctionNoCalled when API call has failed.
completeFunctionNoCalled when API call is complete.

success parameter

NameTypeMandatoryDescription
dataUploadResponseYesInformation returned when the upload task is successful.

fail parameters

NameTypeMandatoryDescription
dataanyYesHeader information returned when the upload task fails.
codenumberYesHTTP status code returned when the upload task fails.

UploadResponse

System capability: SystemCapability.MiscServices.Upload

NameTypeMandatoryDescription
codenumberYesHTTP status code returned by the server.
datastringYesContent returned by the server. The value type is determined by the type in the returned headers.
headersObjectYesHeaders returned by the server.

RequestFile

System capability: SystemCapability.MiscServices.Upload

NameTypeMandatoryDescription
filenamestringNoFile name in the header when multipart is used.
namestringNoName of a form item when multipart is used. The default value is file.
uristringYesLocal path for storing files.
typestringNoType of the file content. By default, the type is obtained based on the extension of the file name or URI.

RequestData

System capability: SystemCapability.MiscServices.Upload

NameTypeMandatoryDescription
namestringYesName of the form element.
valuestringYesValue of the form element.

request.download

download(options: DownloadRequestOptions): void

Downloads a file. This API returns no value.

System capability: SystemCapability.MiscServices.Download

Parameters

NameTypeMandatoryDescription
optionsDownloadRequestOptionsYesDownload configurations.

Example

import request, { DownloadRequestOptions } from '@system.request';

let downloadRequestOptions: DownloadRequestOptions = {
  url: 'http://www.path.com',
  filename: 'requestSystenTest',
  header: "",
  description: 'this is requeSystem download response',
  success: (data:object) => {
    console.info(' download success, code:' + JSON.stringify(data));
  },
  fail: (data:string, code:number) => {
    console.info(' download fail data: ' + data + 'code: ' + code);
  },
  complete: () => {
    console.info(' download complete');
  }
}

try {
  request.download(downloadRequestOptions);
  console.info('download start ');
} catch(err) {
  console.info(' download err:' + err);
}

DownloadRequestOptions

System capability: SystemCapability.MiscServices.Download

NameTypeMandatoryDescription
urlstringYesResource URL.
filenamestringNoName of the file to download. The value is obtained from the current request or resource URL by default.
headerObjectNoRequest header.
descriptionstringNoDownload description. The default value is the file name.
successFunctionNoCalled when API call is successful.
failFunctionNoCalled when API call has failed.
completeFunctionNoCalled when API call is complete.

success parameter

NameTypeMandatoryDescription
dataDownloadResponseYesInformation returned when the download task is successful.

fail parameters

NameTypeMandatoryDescription
dataanyYesHeader information returned when the download task fails.
codenumberYesHTTP status code returned when the download task fails.

DownloadResponse

System capability: SystemCapability.MiscServices.Download

NameTypeMandatoryDescription
tokenstringYesDownload token, which is used to obtain the download status

request.onDownloadComplete

onDownloadComplete(options: OnDownloadCompleteOptions): void

Listens for download task status. This API returns no value.

System capability: SystemCapability.MiscServices.Download

Parameters

NameTypeMandatoryDescription
optionsOnDownloadCompleteOptionsYesConfigurations of the download task.

Example

import request, { OnDownloadCompleteOptions } from '@system.request';

let onDownloadCompleteOptions: OnDownloadCompleteOptions = {
  token: 'token-index',
  success: (data:object) => {
    console.info(' download success, code:' + JSON.stringify(data));
  },
  fail: (data:string, code:number) => {
    console.info(' download fail data: ' + data + 'code: ' + code);
  },
  complete: () => {
    console.info(' download complete');
  }
}

request.onDownloadComplete(onDownloadCompleteOptions);

OnDownloadCompleteOptions

System capability: SystemCapability.MiscServices.Download

NameTypeMandatoryDescription
tokenstringYesResult token returned by the download API.
successFunctionNoCalled when API call is successful.
failFunctionNoCalled when API call has failed.
completeFunctionNoCalled when API call is complete.

success parameter

NameTypeMandatoryDescription
dataOnDownloadCompleteResponseYesInformation returned when the download task is successful.

fail parameters

NameTypeMandatoryDescription
dataanyYesHeader information returned when the download task fails.
codenumberYesHTTP status code returned when the download task fails.

OnDownloadCompleteResponse

System capability: SystemCapability.MiscServices.Download

NameTypeMandatoryDescription
uristringYesURI of the download file.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/ff24b8e711414a41b8184df084f15b8b