Search.../

listFiles( )

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 function 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 (backend page code)

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 */
List files in a specific folder (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { files } from 'wix-media.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample listOptions value:
6 * {
7 * parentFolderId : 'igje5u22nij3qkltzsnol37j3dnthvvh'
8 * }
9 */
10
11export const myListFilesFunction = webMethod(Permissions.Anyone, async (listOptions) => {
12 try {
13 const elevatedListFiles = elevate(files.listFiles);
14 const fileList = await elevatedListFiles(listOptions);
15
16 console.log('Files in folder:', fileList);
17 return fileList;
18 } catch (error) {
19 console.error(error);
20 // Handle the error
21 }
22});
23
24/* Promise resolves to:
25 * {
26 * "files": [
27 * {
28 * "_createdDate": "2023-07-16T08:56:07.000Z",
29 * "_id": "w8ide0_f4dRFF610cKd7zkjVbaNddXgb181lXfg.pdf",
30 * "_updatedDate": "2023-07-16T08:56:07.000Z",
31 * "displayName": "file1.pdf",
32 * "hash": "z3bxokp79p04ok6iiu9ao8srx5lrynfh",
33 * "internalTags": [
34 * "_fileOrigin_uploaded"
35 * ],
36 * "labels": [],
37 * "media": {
38 * "document": "wix:document://v1/w8ide0_f4dRFF610cKd7zkjVbaNddXgb181lXfg.pdf/file1.pdf"
39 * },
40 * "mediaType": "DOCUMENT",
41 * "operationStatus": "READY",
42 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
43 * "private": false,
44 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
45 * "sizeInBytes": "187199",
46 * "state": "OK",
47 * "thumbnailUrl": "https://static.wixstatic.com/media/it7ucst133nc8d9e0ff4d0nay1ucksc6.png",
48 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/ugd/w8ide0_f4dRFF610cKd7zkjVbaNddXgb181lXfg.pdf"
49 * },
50 * {
51 * "_createdDate": "2023-07-16T08:56:07.000Z",
52 * "_id": "w8ide0_170y03n3akmstnpt5jmsju89wvgs9tg4.pdf",
53 * "_updatedDate": "2023-07-16T08:56:07.000Z",
54 * "displayName": "file2.pdf",
55 * "hash": "g6x41coggaystn596ndb09q1ecg036gk",
56 * "internalTags": [
57 * "_fileOrigin_uploaded"
58 * ],
59 * "labels": [],
60 * "media": {
61 * "document": "wix:document://v1/w8ide0_170y03n3akmstnpt5jmsju89wvgs9tg4.pdf/file2.pdf"
62 * },
63 * "mediaType": "DOCUMENT",
64 * "operationStatus": "READY",
65 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
66 * "private": false,
67 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
68 * "sizeInBytes": "81624",
69 * "state": "OK",
70 * "thumbnailUrl": "https://static.wixstatic.com/media/it7ucst133nc8d9e0ff4d0nay1ucksc6.png",
71 * "url": "https://3ecba886-4267-11ee-be56-0242ac120002.usrfiles.com/ugd/w8ide0_170y03n3akmstnpt5jmsju89wvgs9tg4.pdf"
72 * }
73 * ],
74 * "nextCursor": {
75 * "cursors": {
76 * "next": ""
77 * },
78 * "hasNext": false
79 * }
80 * }
81 */
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.web.js *
3 *******************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { files } from 'wix-media.v2';
7import { elevate } from 'wix-auth';
8
9export const generateDownloadUrl = webMethod(Permissions.Anyone, async (fileIds) => {
10 try {
11 const elevatedGenerateFilesDownloadUrl = elevate(files.generateFilesDownloadUrl)
12 const generatedUrl = await elevatedGenerateFilesDownloadUrl(fileIds);
13
14 return generatedUrl.downloadUrl;
15 } catch (error) {
16 console.error(error);
17 }
18});
19
20export const listImages = webMethod(Permissions.Anyone, async () => {
21 const listOptions = {
22 mediaTypes: ["IMAGE"]
23 }
24
25 try {
26 const elevatedListFiles = elevate(files.listFiles)
27 const images = await elevatedListFiles(listOptions);
28
29 return images.files;
30 } catch (error) {
31 console.error(error);
32 }
33});
34
35
36/*************
37 * Page code *
38 ************/
39
40import { listImages, generateDownloadUrl } from 'backend/download-files-url.web';
41
42$w.onReady(() => {
43 $w('#downloadFiles').disable();
44 getDownloadUrl();
45});
46
47async function getDownloadUrl() {
48 const images = await listImages();
49
50 const imageIds = images.map((image) => {
51 return image._id;
52 });
53 const downloadUrl = await generateDownloadUrl(imageIds);
54
55 $w('#downloadFiles').link = downloadUrl;
56 await $w('#downloadFiles').enable();
57}
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.web.js *
3 ************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { files } from 'wix-media.v2';
7import { elevate } from 'wix-auth';
8
9export const updateFile = webMethod(Permissions.Anyone, async (fileId, file) => {
10 try {
11 const elevatedUpdateFileDescriptor = elevate(files.updateFileDescriptor);
12 const updatedDescriptor = await elevatedUpdateFileDescriptor(fileId, file);
13
14 return updatedDescriptor;
15 } catch (error) {
16 console.error(error);
17 }
18});
19
20export const listImages = webMethod(Permissions.Anyone, async () => {
21 const listOptions = {
22 mediaTypes: ["IMAGE"]
23 }
24
25 try {
26 const elevatedListFiles = elevate(files.listFiles)
27 const images = await elevatedListFiles(listOptions);
28
29 return images.files;
30 } catch (error) {
31 console.error(error);
32 }
33});
34
35
36/*************
37 * Page code *
38 ************/
39
40import { updateFile, listImages } from 'backend/update-file.web';
41
42$w.onReady(async () => {
43 await populateImagesDropdown();
44
45 $w('#update').onClick(async () => {
46 const imageId = $w('#imagesDropdown').value;
47
48 const fileUpdates = {
49 displayName: $w('#newDisplayName').value
50 }
51
52 const updatedLocation = await updateFile(imageId, fileUpdates);
53 console.log('Display name successfully updated to:', updatedLocation.displayName)
54 $w('#updateSuccessMsg').show();
55 });
56});
57
58async function populateImagesDropdown() {
59 const images = await listImages();
60 const dropdownOptions = images.map((image) => {
61 return {
62 label: image.displayName,
63 value: image._id
64 };
65 });
66
67 $w('#imagesDropdown').options = dropdownOptions;
68};