Before you use the upload SDK for WeChat mini programs to upload files, you must integrate the SDK. This topic shows you how to integrate the upload SDK for WeChat mini programs and use the SDK to upload a file.
Upload a file
To upload a file by using the upload SDK for WeChat mini programs, perform the following steps:
Import the JavaScript code.
For more information about the download link of the JavaScript code, see Release notes of the upload SDK for mini programs.
import VODUpload from 'aliyun-upload-sdk-1.0.3.min.js'
Obtain an upload URL and an upload credential or an STS token for upload authorization.
The upload SDK for WeChat mini programs supports the following two upload authorization methods:
For more information about how to obtain the upload URL and credential, see Obtain upload URLs and credentials.
For more information about how to obtain an STS token, see Obtain an STS token.
Upload result
Use the obtained upload URL and credential or the obtained STS token to initialize the upload instance.
Use the upload URL and credential or the STS token to initialize an upload instance.
You need to configure the initialization callback before you initialize the upload instance.
Declare a
VODUpload
initialization callback.Initialize the upload instance. You can use an upload URL and an upload credential or an STS token to initialize the upload instance based on your business requirements.
(Recommended) Use the upload URL and credential
Specify the obtained upload URL and credential by calling the
setUploadAuthAndAddress(uploadFileInfo, uploadAuth, uploadAddress,videoId);
method in theonUploadStarted
callback that is invoked when the upload starts.NoteIf the upload credential expires, the
onUploadTokenExpired
callback is invoked. Call theresumeUploadWithAuth(uploadAuth)
method to resume the upload by using a new upload credential.Use the STS token
Specify the obtained STS token by calling the
setSTSToken(uploadInfo, accessKeyId, accessKeySecret, secretToken);
method in theonUploadStarted
callback that is fired when the upload starts.NoteIf the STS token expires, the
onUploadTokenExpired
callback is invoked. Call theresumeUploadWithSTSToken(accessKeyId, accessKeySecret, secretToken);
method to resume the upload by using a new STS token.
Set upload parameters based on the type of the file that you want to upload. The file can be an audio file, a video file, or an image file.
Upload parameters for audio and video files
Set the upload parameters for adding an audio or video file to the upload list.
wx.chooseVideo({ success: function (res) { var file = {url: res.tempFilePath, coverUrl: res.thumbTempFilePath}; var userData = '{"Vod":{}}'; uploader.addFile(file, null, null, null, userData) } })
During the upload, you can use
paramData
to customize audio and video information.paramData
is a JSON string. You must setVod
at the first layer. Add attributes thatparamData
supports withinVod
. For more information about supported attributes, see CreateUploadVideo. Sample code:var userData = '{"Vod":{"Title":"test","CateId":"234"}}';
Upload parameters for image files
Set the upload parameters for adding an image file to the upload list.
wx.chooseImage({ success: function (res) { var file = {url: res.tempFilePath, coverUrl: res.thumbTempFilePath}; var userData = '{"Vod":{}}'; uploader.addFile(file, null, null, null, userData) } })
Start the upload.
Call the
startUpload()
method to start the upload.uploader.startUpload();
When the upload starts, the
onUploadProgress
callback is invoked to synchronize the upload progress.When the file is uploaded, the
onUploadSucceed
callback is invoked to return the upload result.
Upload result
After a video is uploaded, the videoId parameter is returned, which indicates the video ID. Then, you can obtain the streaming URL based on the video ID to play the video. For more information, see Use playback credentials to play videos.
After an image is uploaded, the imageUrl parameter is returned. The value of the imageUrl parameter indicates the image URL. If URL signing is enabled, the image URL expires after a specific period of time. For more information, see Configure URL signing.
Manage the upload list
Remove a file to be uploaded.
The index parameter indicates the index of a file in the list returned by the listFiles method.
uploader.deleteFile(index);
Cancel the upload of a single file.
uploader.cancelFile(index);
NoteYou can use this method to cancel the upload of a file that is being uploaded or waiting to be uploaded.
Resume the upload of a single file.
uploader.resumeFile(index);
Query the upload list.
uploader.listFiles();
Clear the upload list.
uploader.cleanList();
Manage the upload credential or STS token
Resume the upload after the upload credential expires.
uploader.resumeUploadWithAuth(uploadAuth);
Specify the upload URL and credential.
Set the upload URL and credential in the
onUploadstarted
callback.uploadInfo
is included in the callback.uploader.setUploadAuthAndAddress(uploadInfo,uploadAuth, uploadAddress, videoId);
Specify the STS token.
Specify the STS token in the
onUploadstarted
callback.uploadInfo
is included in the callback.uploader.setSTSToken(uploadInfo, accessKeyId, accessKeySecret,secretToken);
Resume the upload after the STS token expires.
uploader.resumeUploadWithSTSToken(accessKeyId, accessKeySecret, secretToken, expireTime);