Skip to content

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。

    PropertiesTypeDefault valueRequiredDescription
    urlstring-YesDeveloper server address
    filePathstring-YesPath of the file resource to be uploaded (local path)
    namestring-YesKey corresponding to the file, developers can use this key to obtain the binary content of the file on the server
    headerobject-NoHTTP request Header, Referer cannot be set in the Header
    formDataobject-NoOther additional form data in the HTTP request
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    datastringData returned by the developer server
    statusCodenumberHTTP 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:

js
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:

    PropertiesTypeDescription
    progressnumberDownload progress percentage
    totalBytesWrittennumberThe length of the data that has been downloaded, in Bytes
    totalBytesExpectedToWritenumberThe 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:

js
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.

    PropertiesTypeDescription
    headerObjectHTTP 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:

js
const listener = function (res) { console.log(res) }

UploadTask.onHeadersReceived(listener)
UploadTask.offHeadersReceived(listener)  // Pass the same function object used for listening.
js
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