VOD provides an SDK for uploading files from browsers. You can download the SDK source code at GitHub.
If Webpack is not used, you can import the SDK using a script tag. This method will expose the global variable TcVod
. You can choose either of the two ways below:
<script src="./vod-js-sdk-v6.js"></script>
Note:Change the value of
src
to the local path of the source code.
<script src="https://cdn-go.cn/cdn/vod-js-sdk-v6/latest/vod-js-sdk-v6.js"></script>
Click here to try a demo that imported the SDK using a script tag. The source code of the demo can be found here.
If Webpack (such as Vue or React) is used, You can use npm to import the SDK:
// Run `npm install vod-js-sdk-v6`, and use the command below to import the SDK directly on the page:
import TcVod from 'vod-js-sdk-v6'
Click here to view the source code of a demo that imports the SDK using npm.
Note:The SDK relies on promises, which you should import if your browser version is old.
function getSignature() {
return axios.post(url).then(function (response) {
return response.data.signature;
})
};
Note:
url
is the URL of your signature distribution service. For more information, see the Guide for upload from a client.- For details on how to calculate
signature
, see Signature for Upload from Client.- The upload signature contains information such as the subapplication ID, video category, and task flow. For more information, see Descriptions of Signature Parameters.
// If the SDK is imported using the `import` command, run `new TcVod(opts)`.
// If the SDK is imported using a script tag, use `new TcVod.default(opts)`.
const tcVod = new TcVod.default({
getSignature: getSignature // The function to get the upload signature
})
const uploader = tcVod.upload({
mediaFile: mediaFile, // The media file (video, audio, or image), whose data type is file.
})
uploader.on('media_progress', function(info) {
console.log(info.percent) // The upload progress
})
// Callback of the result
// type doneResult = {
// fileId: string,
// video: {
// url: string
// },
// cover: {
// url: string
// }
// }
uploader.done().then(function (doneResult) {
// Deal with doneResult
}).catch(function (err) {
// Deal with error
})
Note:
opts
innew TcVod(opts)
refers to parameters of theTcVod
API. For details, see API Description.- The upload API automatically selects simple upload or multipart upload based on the file size. You don’t need to manually set up multipart upload.
- To upload to a subapplication, see Subapplication System - Upload from client.
const uploader = tcVod.upload({
mediaFile: mediaFile,
coverFile: coverFile,
})
uploader.done().then(function (doneResult) {
// Deal with doneResult
})
The SDK can notify you of the upload progress via callbacks:
const uploader = tcVod.upload({
mediaFile: mediaFile,
coverFile: coverFile,
})
// When the video upload is completed
uploader.on('media_upload', function(info) {
uploaderInfo.isVideoUploadSuccess = true;
})
// The video upload progress
uploader.on('media_progress', function(info) {
uploaderInfo.progress = info.percent;
})
// When the thumbnail upload is completed
uploader.on('cover_upload', function(info) {
uploaderInfo.isCoverUploadSuccess = true;
})
// The thumbnail upload progress
uploader.on('cover_progress', function(info) {
uploaderInfo.coverProgress = info.percent;
})
uploader.done().then(function (doneResult) {
// Deal with doneResult
})
For details about the return values of xxx_upload
and xxx_progress
, see Object Operations.
The SDK supports canceling ongoing video or thumbnail upload:
const uploader = tcVod.upload({
mediaFile: mediaFile,
coverFile: coverFile,
})
uploader.cancel()
The SDK supports automatic checkpoint restart for uploads. If an upload is interrupted unexpectedly (for example, because the browser is closed or the network is disconnected), you can continue uploading the file from where it left off.
Parameter | Required | Type | Description |
---|---|---|---|
getSignature | Yes | Function | The function used to get the upload signature. |
appId | No | number | If this parameter is set, it will be carried by the built-in statistical report system. |
reportId | No | number | If this parameter is set, it will be carried by the built-in statistical report system. |
Parameter | Required | Type | Description |
---|---|---|---|
mediaFile | No | File | The media file (video, audio, or image). |
coverFile | No | File | The thumbnail file. |
mediaName | No | string | The filename, which will overwrite the filename in the metadata. |
fileId | No | string | The ID of the new thumbnail file. |
reportId | No | number | If this parameter is set, it will be carried by the built-in statistical report system and will overwrite the settings in the constructor. |
fileParallelLimit | No | number | The maximum number of concurrent uploads allowed in the same instance. Default value: 3. |
chunkParallelLimit | No | number | The maximum number of upload parts allowed for the same file. Default value: 6. |
chunkRetryTimes | No | number | The maximum number of retry attempts for multipart upload. Default value: 2 (three upload requests in total). |
chunkSize | No | number | The part size (bytes) for multipart upload. Default value: 8388608 (8 MB). |
progressInterval | No | number | The interval (ms) of sending the onProgress callback. Default value: 1000. |
Event Name | Required | Description |
---|---|---|
media_upload | No | The media file is successfully uploaded. |
cover_upload | No | The thumbnail is successfully uploaded. |
media_progress | No | The media file upload progress. |
cover_progress | No | The thumbnail file upload progress. |
input
tag and set type
to file
.uploader.cancel()
, and to resume an upload after pause, call tcVod.upload
. Note that when you use tcVod.upload
to resume an upload, you need to pass in the same parameters used when you initiate the upload (you can use a global variable to save the parameters when you initiate the upload and delete them after upload.)https:
upload?http:
for upload on HTTP pages and https:
on non-HTTP pages.
Was this page helpful?