Upload
uploadFile
TIP
The API usage is as follows: UploadTask wx.uploadFile(Object object)
Functional description: Upload local resources to the server. The client initiates an HTTPS POST request with content-type as multipart/form-data. Please read carefully before useRelated instructions.
Parameters and descriptions: Object object。
Properties Type Default value Required Description url string - Yes Developer server address filePath string - Yes Path of the file resource to be uploaded (local path) name string - Yes Key corresponding to the file, developers can use this key to obtain the binary content of the file on the server header object - No HTTP request Header, Referer cannot be set in the Header formData object - No Other additional form data in the HTTP request success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) object.success callback function parameters: Object res。
Properties Type Description data string Data returned by the developer server statusCode number HTTP status code returned by the developer server Return value: UploadTask An object that can monitor the upload progress change event 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
TIP
The API usage is as follows: UploadTask.abort()
- Functional description: Interrupt the upload task.
.onProgressUpdate
TIP
The API usage is as follows: UploadTask.onProgressUpdate(function listener)
Functional description: Listen for the upload progress change event.
Parameters and descriptions: function listener,Listener function for the upload progress change event, parameter Object.res is as follows:
Properties Type Description progress number Download progress percentage totalBytesWritten number The length of the data that has been downloaded, in Bytes totalBytesExpectedToWrite number The total length of the data expected to be downloaded, in Bytes
.offProgressUpdate
TIP
The API usage is as follows: UploadTask.offProgressUpdate(function listener)
Functional description: Remove the listener function for the upload progress change event.
Parameters and descriptions: function listener, onProgressUpdate The listener function passed in. If this parameter is not passed, all listeners will be removed.
Sample code:
const listener = function (res) { console.log(res) }
DownloadTask.onProgressUpdate(listener)
DownloadTask.offProgressUpdate(listener) // Pass the same function object used for listening..onHeadersReceived
TIP
The API usage is as follows: UploadTask.onHeadersReceived(function listener)
Functional description: Listen to the HTTP Response Header event. It will be earlier than the request completion event.
Parameters and descriptions: function listener,Listener function of the HTTP Response Header event.
Properties Type Description header Object HTTP Response Header returned by the developer server
.offHeadersReceived
TIP
The API usage is as follows: UploadTask.offHeadersReceived(function listener)
Functional description: Remove the listener function of the HTTP Response Header event.
Parameters and descriptions: function listener, onHeadersReceived The listener function passed in. If this parameter is not passed, all listeners will be removed.
Sample code:
const listener = function (res) { console.log(res) }
UploadTask.onHeadersReceived(listener)
UploadTask.offHeadersReceived(listener) // Pass the same function object used for listening.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