tencent cloud

All product documents
Tencent Cloud Super App as a Service
Last updated: 2025-03-20 18:01:56
Upload
Last updated: 2025-03-20 18:01:56

uploadFile

This API is used via UploadTask wx.uploadFile(Object object).
Feature Description: Uploads local resources to the server. The client initiates an HTTPS POST request, where the content-type is multipart/form-data. Prior to usage, read relevant instructions.
Parameter and Description: Object.
Attribute
Type
Default value
Required
Description
url
string
-
Yes
Developer Server Address
filePath
string
-
Yes
Path to the file resource to be uploaded (local path)
name
string
-
Yes
The corresponding key for the file, through which developers can access the binary content of the file on the server side.
header
object
-
No
HTTP request's Header; setting Referer within the Header is not allowed.
formData
object
-
No
Additional form data in the HTTP request
success
function
-
No
Callback Function of Successful Interface Call
fail
function
-
No
Callback Function of Failing Interface Call
complete
function
-
No
Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations)
Parameters for object.success callback function: Object res.
Attribute
Type
Description
data
string
Data returned from the developer's server.
statusCode
number
HTTP status code returned from the developer's server.
Return Value: UploadTask, an object that can monitor the progress of the upload and cancel the upload.
Sample Code
wx.chooseImage({
success (res){
const tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'https://example.weixin.qq.com/upload', // This is merely an example and not an actual interface address.
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success (res){
const data = res.data
//do something
}
})
}
})

UploadTask

.abort

This method is used via UploadTask.abort().
Feature Description: Aborts the upload task.

.onProgressUpdate

This method is used via UploadTask.onProgressUpdate(function listener).
Feature Description: Monitors upload progress change events.
Parameter and Description: function listener, listener for upload progress change events, arguments Object.res are as follows.
Attribute
Type
Description
progress
number
Download progress percentage
totalBytesWritten
number
Length of the data already downloaded, measured in Bytes.
totalBytesExpectedToWrite
number
Total length of the data expected to be downloaded, measured in bytes.

.offProgressUpdate

This method is used via UploadTask.offProgressUpdate(function listener).
Feature Description: Removes the listener function for upload progress change events.
Parameters and Description: function listener, the listener function passed in by onProgressUpdate. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

DownloadTask.onProgressUpdate(listener)
DownloadTask.offProgressUpdate(listener) // The same function object as the listener must be passed in.

.onHeadersReceived

This method is used via UploadTask.onHeadersReceived(function listener).
Feature Description: Monitors HTTP Response Header events. This occurs earlier than the request completion event.
Parameter and Description: function listener, the listener function for HTTP Response Header events.
Attribute
Type
Description
header
Object
HTTP Response Header returned from the developer's server.

.offHeadersReceived

This method is used via UploadTask.offHeadersReceived(function listener).
Feature Description: Removes the listener function for HTTP Response Header events.
Parameters and Description: function listener, the listener function passed in by onHeadersReceived. If this parameter is not passed in, all listener functions will be removed.
Sample Code
const listener = function (res) { console.log(res) }

UploadTask.onHeadersReceived(listener)
UploadTask.offHeadersReceived(listener) // The same function object as the listener must be passed in.

Sample code

const uploadTask = wx.uploadFile({
url: 'http://example.weixin.qq.com/upload', // This is merely an example and not an actual interface address.
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success (res){
const data = res.data
//do something
}
})

uploadTask.onProgressUpdate((res) => {
console.log('Upload progress', res.progress)
console.log('Length of already uploaded data', res.totalBytesSent)
console.log('Total length of data expected to be uploaded', res.totalBytesExpectedToSend)
})

uploadTask.abort() // Aborts upload task


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support