Search.../

listFiles( )

Developer Preview

Retrieves a list of files in the Media Manager.

Description

The listFiles() function returns a Promise that resolves to an array of the specified files' descriptors and cursor information.

To retrieve a list of files within a specific folder in the Media Manager, pass the folder's ID in the parentFolderId parameter. If no folder is specified, the endpoint retrieves only the list of files in the root folder of the Media Manager.

To retrieve a list of (non-permanently) deleted files, use the listDeletedFiles() function.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function listFiles(options: ListFilesOptions): Promise<ListFilesResponse>

listFiles Parameters

NAME
TYPE
DESCRIPTION
options
Optional
ListFilesOptions

Options to use when listing media files.

Returns

Return Type:

Promise<
ListFilesResponse
>
NAME
TYPE
DESCRIPTION
files
Array<
FileDescriptor
>

List of files in the Media Manager.

nextCursor
PagingMetadataV2

The next cursor if it exists.

Was this helpful?

List files in a specific folder

Copy Code
1import { files } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample listOptions value:
5 * {
6 * parentFolderId : 'igje5u22nij3qkltzsnol37j3dnthvvh'
7 * }
8 */
9
10async function myListFilesFunction(listOptions) {
11 try {
12 const elevatedListFiles = elevate(files.listFiles);
13 const fileList = await elevatedListFiles(listOptions);
14
15 console.log('Files in folder:', fileList);
16 return fileList;
17 } catch (error) {
18 console.error(error);
19 // Handle the error
20 }
21}
22
23/* Promise resolves to:
24 * {
25 * "files": [
26 * {
27 * "_createdDate": "2023-07-16T08:56:07.000Z",
28 * "_id": "w8ide0_f4dRFF610cKd7zkjVbaNddXgb181lXfg.pdf",
29 * "_updatedDate": "2023-07-16T08:56:07.000Z",
30 * "displayName": "file1.pdf",
31 * "hash": "z3bxokp79p04ok6iiu9ao8srx5lrynfh",
32 * "internalTags": [
33 * "_fileOrigin_uploaded"
34 * ],
35 * "labels": [],
36 * "media": {
37 * "document": "wix:document://v1/w8ide0_f4dRFF610cKd7zkjVbaNddXgb181lXfg.pdf/file1.pdf"
38 * },
39 * "mediaType": "DOCUMENT",
40 * "operationStatus": "READY",
41 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
42 * "private": false,
43 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
44 * "sizeInBytes": "187199",
45 * "state": "OK",
46 * "thumbnailUrl": "https://static.wixstatic.com/media/it7ucst133nc8d9e0ff4d0nay1ucksc6.png",
47 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/ugd/w8ide0_f4dRFF610cKd7zkjVbaNddXgb181lXfg.pdf"
48 * },
49 * {
50 * "_createdDate": "2023-07-16T08:56:07.000Z",
51 * "_id": "w8ide0_170y03n3akmstnpt5jmsju89wvgs9tg4.pdf",
52 * "_updatedDate": "2023-07-16T08:56:07.000Z",
53 * "displayName": "file2.pdf",
54 * "hash": "g6x41coggaystn596ndb09q1ecg036gk",
55 * "internalTags": [
56 * "_fileOrigin_uploaded"
57 * ],
58 * "labels": [],
59 * "media": {
60 * "document": "wix:document://v1/w8ide0_170y03n3akmstnpt5jmsju89wvgs9tg4.pdf/file2.pdf"
61 * },
62 * "mediaType": "DOCUMENT",
63 * "operationStatus": "READY",
64 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
65 * "private": false,
66 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
67 * "sizeInBytes": "81624",
68 * "state": "OK",
69 * "thumbnailUrl": "https://static.wixstatic.com/media/it7ucst133nc8d9e0ff4d0nay1ucksc6.png",
70 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/ugd/w8ide0_170y03n3akmstnpt5jmsju89wvgs9tg4.pdf"
71 * }
72 * ],
73 * "nextCursor": {
74 * "cursors": {
75 * "next": ""
76 * },
77 * "hasNext": false
78 * }
79 * }
80 */
Generate a download url for all images in media

This is an example of a function running on page ready. The function lists all the images from media, and then uses their IDs to create a download url.

Copy Code
1/**************************************
2 * Backend code - download-files-url.jsw *
3 *************************************/
4
5import { files } from 'wix-media.v2';
6import { elevate } from 'wix-auth';
7
8export async function generateDownloadUrl(fileIds) {
9 try {
10 const elevatedGenerateFilesDownloadUrl = elevate(files.generateFilesDownloadUrl)
11 const generatedUrl = await elevatedGenerateFilesDownloadUrl(fileIds);
12
13 return generatedUrl.downloadUrl;
14 } catch (error) {
15 console.error(error);
16 }
17}
18
19export async function listImages() {
20 const listOptions = {
21 mediaTypes: ["IMAGE"]
22 }
23
24 try {
25 const elevatedListFiles = elevate(files.listFiles)
26 const images = await elevatedListFiles(listOptions);
27
28 return images.files;
29 } catch (error) {
30 console.error(error);
31 }
32}
33
34
35/*************
36 * Page code *
37 ************/
38
39import { listImages, generateDownloadUrl } from 'backend/download-files-url';
40
41$w.onReady(() => {
42 $w('#downloadFiles').disable();
43 getDownloadUrl();
44});
45
46async function getDownloadUrl() {
47 const images = await listImages();
48
49 const imageIds = images.map((image) => {
50 return image._id;
51 });
52 const downloadUrl = await generateDownloadUrl(imageIds);
53
54 $w('#downloadFiles').link = downloadUrl;
55 await $w('#downloadFiles').enable();
56}
Update a file's display name from visitor input

This code uses the visitor's input on the page to update the display name of an image which they choose from a dropdown list on the page.

Copy Code
1/**********************************
2 * Backend code - update-file.jsw *
3 *********************************/
4
5import { files } from 'wix-media.v2';
6import { elevate } from 'wix-auth';
7
8export async function updateFile(fileId, file) {
9 try {
10 const elevatedUpdateFileDescriptor = elevate(files.updateFileDescriptor);
11 const updatedDescriptor = await elevatedUpdateFileDescriptor(fileId, file);
12
13 return updatedDescriptor;
14 } catch (error) {
15 console.error(error);
16 }
17}
18
19export async function listImages() {
20 const listOptions = {
21 mediaTypes: ["IMAGE"]
22 }
23
24 try {
25 const elevatedListFiles = elevate(files.listFiles)
26 const images = await elevatedListFiles(listOptions);
27
28 return images.files;
29 } catch (error) {
30 console.error(error);
31 }
32}
33
34
35/*************
36 * Page code *
37 ************/
38
39import { updateFile, listImages } from 'backend/update-file';
40
41$w.onReady(async () => {
42 await populateImagesDropdown();
43
44 $w('#update').onClick(async () => {
45 const imageId = $w('#imagesDropdown').value;
46
47 const fileUpdates = {
48 displayName: $w('#newDisplayName').value
49 }
50
51 const updatedLocation = await updateFile(imageId, fileUpdates);
52 console.log('Display name successfully updated to:', updatedLocation.displayName)
53 $w('#updateSuccessMsg').show();
54 });
55});
56
57async function populateImagesDropdown() {
58 const images = await listImages();
59 const dropdownOptions = images.map((image) => {
60 return {
61 label: image.displayName,
62 value: image._id
63 };
64 });
65
66 $w('#imagesDropdown').options = dropdownOptions;
67};
68