openharmony 鸿蒙 js-apis-system-request

2025-06-12 浏览 (1)

@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(deprecated)

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(deprecated)

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(deprecated)

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(deprecated)

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(deprecated)

System capability: SystemCapability.MiscServices.Upload

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

request.download(deprecated)

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(deprecated)

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(deprecated)

System capability: SystemCapability.MiscServices.Download

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

request.onDownloadComplete(deprecated)

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(deprecated)

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(deprecated)

System capability: SystemCapability.MiscServices.Download

NameTypeMandatoryDescription
uristringYesURI of the download file.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

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