my.uploadFile uploads the local resource to the server.
Supported types of mini programs: DSL, H5+
my.uploadFile({
url: "Please use your own server address",
fileType: "image",
fileName: "file",
filePath: "...",
success: (res) => {
my.alert({
content: "Upload success",
});
},
});
| Property | Type | Required | Description |
|---|---|---|---|
url | String | Yes | Address of the developer server. |
filePath | String | Yes | Local location of the file resource to be uploaded. |
fileName | String | Yes | Filename, also the corresponding key. The developer uses this key at the server side to get the file binary content. |
fileType | String | Yes | The 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. |
header | Object | No | The 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. |
formData | Object | No | Other additional form data in the HTTP request. |
timeout | Object | No | The timeout period of your request in milliseconds. The default value is 60000. |
hideLoading | Boolean | No | Whether 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. |
success | Function | No | Callback function upon call success. |
fail | Function | No | Callback function upon call failure. |
complete | Function | No | Callback function upon call completion (to be executed upon either call success or failure). |
The incoming parameter is of the Object type with the following attributes:
| Property | Type | Description |
|---|---|---|
data | String | Data returned from the server. |
statusCode | String | HTTP status code. |
header | Object | Header returned from the server. |
| Error | Description |
|---|---|
11 | File nonexistent. |
12 | File uploading failed. |
13 | No right. |
fileName parameter specifies the form field name that the server will use to access the fileContent-Type is multipart/form-data - do not change this valueformData to send additional data along with the filehideLoading: true to prevent the default loading indicator from appearingmy.chooseFileFromDisk or my.chooseImagemy.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");
},
});