Resumable Upload API

This article demonstrates how to use the response object from the Generate File Resumable Upload Url endpoint to upload a file to a site's Media Manager.

Authorization

This endpoint uses the uploadToken from the response for authorization. No additional authorization is needed.

Syntax

Copy
1
PUT {{generateFileResumableUploadUrlResponse.uploadUrl}}/{{generateFileResumableUploadUrlResponse.uploadToken}}

Query Params

NameTypeOptionalDescription
filenamestringnoFile name that appears in the Media Manager. Include the file's extension in the name to prevent potential errors.

Example

Implement a Resumable Upload Client using TUS Protocol

In this example we use tus-js-client to implement a resumable upload using the TUS protocol.

Request

Copy
1
async function resumableFileUpload(resumableUploadUrlResponse: GenerateFileResumableUploadUrlResponse): Upload {
2
// get the file content to upload
3
const fileName = 'imageExample.jpg';
4
const fileContent = await fs.createReadStream(path.join(__dirname, '..', 'files', fileName));
5
6
const mimeType = 'image/jpeg';
7
8
const params = {
9
filename: fileName,
10
contentType: mimeType,
11
token: resumableUploadUrlResponse.uploadToken
12
};
13
14
// Wrap the resumable upload session in a promise to wait for the upload to finish.
15
await new Promise(async (resolve, reject) => {
16
// Create a new TUS upload.
17
const upload = new tus.Upload(fileContent, {
18
// Use the uploadUrl from the response of the generate URL endpoint.
19
endpoint: resumableUploadUrlResponse.uploadUrl,
20
// Enable tus-js-client to automatically retry on errors.
21
retryDelays: [0, 3000, 5000, 10000, 20000],
22
// TSend parameters to the upload server to indentify the file and authentication token.
23
metadata: {
24
filename: fileName,
25
contentType: mimeType,
26
token: resumableUploadUrlResponse.uploadToken
27
},
28
// Callback for errors that can't be fixed using retries.
29
onError: function (error) {
30
console.log('Failed because: ' + error);
31
reject(error);
32
},
33
// Callback for reporting upload progress.
34
onProgress: function (bytesUploaded, bytesTotal) {
35
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);
36
console.log(bytesUploaded, bytesTotal, percentage + '%');
37
},
38
// Callback for once the upload is completed.
39
onSuccess: function () {
40
console.log('Download %s from %s', fileName, upload.url);
41
resolve(true);
42
}
43
});
44
45
upload.start();
46
});
47
48
// PUT request to finalize the upload.
49
// Note that we concatenate the URL and token of the resumable upload response.
50
const result = await httpClient.put(
51
`${resumableUploadUrlResponse.uploadUrl}/${resumableUploadUrlResponse.uploadToken}`,
52
{}, { params: { filename: fileName } }
53
);
54
}

Response

Copy
1
{
2
"file": {
3
"id": "2acbb8_86485e224dd84143ba2f228777217bb7~mv2.jpeg",
4
"displayName": "file.jpg",
5
"url": "https://static.wixstatic.com/media/2acbb8_86485e224dd84143ba2f228777217bb7~mv2.jpeg",
6
"parentFolderId": "media-root",
7
"hash": "cf96f0567ed967f02bc9580ab8db86be",
8
"sizeInBytes": "15359",
9
"private": false,
10
"mediaType": "IMAGE",
11
"media": {
12
"image": {
13
"image": {
14
"id": "2acbb8_86485e224dd84143ba2f228777217bb7~mv2.jpeg",
15
"url": "https://static.wixstatic.com/media/2acbb8_86485e224dd84143ba2f228777217bb7~mv2.jpeg",
16
"height": 226,
17
"width": 370,
18
"filename": "file.jpg",
19
"sizeInBytes": "15359"
20
},
21
"faces": []
22
}
23
},
24
"operationStatus": "READY",
25
"thumbnailUrl": "https://static.wixstatic.com/media/2acbb8_86485e224dd84143ba2f228777217bb7~mv2.jpeg",
26
"labels": [],
27
"createdDate": "2022-09-11T15:13:24.000Z",
28
"updatedDate": "2022-09-11T15:13:24.000Z"
29
}
30
}

Status/Error Codes

The response will include an HTTP status code.

Was this helpful?
Yes
No