Network APIs

uploadFile

Upload local resource to the server.

Overview

my.uploadFile uploads the local resource to the server.

Supported types of mini programs: DSL, H5+

Sample Code

my.uploadFile({
  url: "Please use your own server address",
  fileType: "image",
  fileName: "file",
  filePath: "...",
  success: (res) => {
    my.alert({
      content: "Upload success",
    });
  },
});

Parameters

PropertyTypeRequiredDescription
urlStringYesAddress of the developer server.
filePathStringYesLocal location of the file resource to be uploaded.
fileNameStringYesFilename, also the corresponding key. The developer uses this key at the server side to get the file binary content.
fileTypeStringYesThe file type of the uploaded file.

Note: This parameter is only used for backward compatibility with older super app versions. If you have previously set a specific value, no changes are necessary. If not, you can set the value to image for any file type.
headerObjectNoThe HTTP request header. You can specify this parameter using any common headers.

Note: By default, the Content-Type request header is set to multipart/form-data. Do not change this value, otherwise your request will fail.
formDataObjectNoOther additional form data in the HTTP request.
timeoutObjectNoThe timeout period of your request in milliseconds. The default value is 60000.
hideLoadingBooleanNoWhether to hide a loading icon during the uploading process. Valid values are:
true: Hide a loading icon.
false: Show a loading icon.

If not specified, the value defaults to false.
successFunctionNoCallback function upon call success.
failFunctionNoCallback function upon call failure.
completeFunctionNoCallback function upon call completion (to be executed upon either call success or failure).

Success Callback Function

The incoming parameter is of the Object type with the following attributes:

PropertyTypeDescription
dataStringData returned from the server.
statusCodeStringHTTP status code.
headerObjectHeader returned from the server.

Error Code

ErrorDescription
11File nonexistent.
12File uploading failed.
13No right.

Usage Notes

  • Domain whitelist must be configured before uploading files
  • The fileName parameter specifies the form field name that the server will use to access the file
  • The default Content-Type is multipart/form-data - do not change this value
  • Use formData to send additional data along with the file
  • The default timeout is 60000ms (60 seconds)
  • Set hideLoading: true to prevent the default loading indicator from appearing
  • The file path should be obtained from other APIs like my.chooseFileFromDisk or my.chooseImage

Example Usage

Complete Example with Error Handling

my.uploadFile({
  url: "https://your-server.com/api/upload",
  filePath: filePath, // From my.chooseFileFromDisk
  fileName: "file",
  fileType: "PDF",
  formData: {
    userId: "user-123",
    documentType: "invoice",
    timestamp: new Date().toISOString(),
  },
  success: (uploadRes) => {
    console.log("Upload successful");
    console.log("Status Code:", uploadRes.statusCode);
    console.log("Response Data:", uploadRes.data);

    my.alert({
      title: "Success",
      content: "File uploaded successfully!",
    });
  },
  fail: (error) => {
    console.error("Upload failed:", error);

    let errorMessage = "Upload failed";
    if (error.error === 11) {
      errorMessage = "File does not exist";
    } else if (error.error === 12) {
      errorMessage = "File uploading failed";
    } else if (error.error === 13) {
      errorMessage = "No upload permission";
    }

    my.alert({
      title: "Upload Error",
      content: errorMessage,
    });
  },
  complete: () => {
    console.log("Upload process complete");
  },
});