uploadFile(params: Object)

Requires:
BaseKit≥2.0.1@ray-js/ray≥0.6.5
Platforms:
iOSAndroid
import { uploadFile } from '@ray-js/ray';

// Promise usage
try {
  const res = await uploadFile({ ...params });
  console.log(res);
} catch (err) {
  console.log(err.errorMsg);
}

Description

Upload a local resource to the server. The client sends an HTTPS POST request with content-type multipart/form-data

Parameters

PropertyTypeRequiredDefaultDescriptionBaseKit Since
urlstringYes-Developer server URL-
filePathstringYes-Path of the file resource to upload (local path)-
namestringYes-The key for the file; the server can use this key to obtain the file’s binary content-
No-HTTP request headers; Referer must not be set in the headers-
No-Additional form data in the HTTP request-
timeoutnumberNo-Timeout in milliseconds-
method"POST" | "PUT"No'POST'HTTP method for the upload request; defaults to POSTv3.23.0
complete() => voidNo-Completion callback (executed on both success and failure)-
success(params: Object) => voidNo-Callback on successful API call-
fail(params: Object) => voidNo-Failure callback-
success callback parameters
PropertyTypeDescriptionBaseKit Since
datastringData returned by the developer serverv3.2.6
statusCodenumberHTTP status code returned by the developer serverv3.2.6
fail callback parameters
PropertyTypeDescription
errorMsgstringError message
errorCodestring | numberError code
Error extension

Return Value

Type: UploadTask
UploadTask
PropertyTypeDescription
abortfunctionAbort upload task
onHeadersReceivedfunctionListen for HTTP Response Header events. Fires earlier than the request completion event
offHeadersReceivedfunctionStop listening for HTTP Response Header events
onProgressUpdatefunctionListen for upload progress change events
offProgressUpdatefunctionStop listening for upload progress change events

Valid Values

method
ValueDescriptionBaseKit Since
POSTHTTP POST requestv3.23.0
PUTHTTP PUT requestv3.23.0

Examples

import { downloadFile, uploadFile } from '@ray-js/ray'

// Upload a local file: download a file to get a local path, then use uploadFile to upload it to the server; returns an UploadTask
downloadFile({
  url: "https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/ttttestfile/test_image.jpg",
  success: ({ tempFilePath }) => {
    const task = uploadFile({
      url: "https://httpbin.org/post",
      filePath: tempFilePath,
      name: "file",
      success: data => console.log("Upload completed, status code:", data.statusCode),
      fail: error => console.error("Upload failed:", JSON.stringify(error)),
    });
    console.log("UploadTask created");
  },
  fail: error => console.error(error),
});