dlt.uploadFile
Introduction
Uploads local resources to a server. The client initiates an HTTPS POST request with the content-type
set to multipart/form-data
.
Parameters
Object object
Attribute | Type | Default | Required | Description | Minimum Version |
---|---|---|---|---|---|
url | string | Yes | The request URL. | ||
header | string | No | The request header in JSON string format. | ||
formData | string/Object | No | Other additional form data in the HTTP request. If it's an Object, make sure the values are of string type. | ||
filePath | string | No | The path where the downloaded file will be stored (relative path). | 3.0.0 | |
timeout | number | 60000 | No | Timeout duration in milliseconds. The default value is 60000 (60 seconds). | 3.0.0 |
success | function | No | The callback function for a successful API call. | ||
fail | function | No | The callback function for a failed API call. | ||
complete | function | No | The callback function for the completion of the API call (executed for both successful and failed calls). |
object.success Callback Function
Parameters
Object res
Attribute | Type | Description |
---|---|---|
response | string | Information from the server. |
success | string | true if successful. |
object.fail Callback Function
Parameters
Object res
Attribute | Type | Description |
---|---|---|
errCode | string | Error code. |
errMsg | string | Error message. |
success | string | false if failed. |
errMsg Explanation
Error Message | Description |
---|---|
UploadFile fail, url is not startsWith http:U001 | Upload failed, invalid URL. |
UploadFile fail, Exception_"+t.toString()+":U002 | Upload failed, an exception occurred. |
UploadFile fail, file:" + localPath + " is not exist:U003 | Upload failed, the file address to upload doesn't exist. |
UploadFile fail, parse: header/formData error:U004 | Upload failed, there's an error in header or formData. |
UploadFile fail, onFailure_${e.toString()}:U005 | Upload failed. |
UploadFile fail, onResponse_${e.toString()}:U006 | Upload failed. |
Sample Code
// pages/index/index.js
Page({
uploadFile() {
dlt.uploadFile({
url: "xxx",
filePath: tempFilePaths[0],
formData: {
user: "test",
},
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
},
});
},
});