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.
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 to use when listing media files.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
List of files in the Media Manager.
The next cursor if it exists.
Was this helpful?
1import { files } from 'wix-media.v2';2import { elevate } from 'wix-auth';34/* Sample listOptions value:5 * {6 * parentFolderId : 'igje5u22nij3qkltzsnol37j3dnthvvh'7 * }8 */910async function myListFilesFunction(listOptions) {11 try {12 const elevatedListFiles = elevate(files.listFiles);13 const fileList = await elevatedListFiles(listOptions);1415 console.log('Files in folder:', fileList);16 return fileList;17 } catch (error) {18 console.error(error);19 // Handle the error20 }21}2223/* 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": false78 * }79 * }80 */
1import { Permissions, webMethod } from 'wix-web-module';2import { files } from 'wix-media.v2';3import { elevate } from 'wix-auth';45/* Sample listOptions value:6 * {7 * parentFolderId : 'igje5u22nij3qkltzsnol37j3dnthvvh'8 * }9 */1011export const myListFilesFunction = webMethod(Permissions.Anyone, async (listOptions) => {12 try {13 const elevatedListFiles = elevate(files.listFiles);14 const fileList = await elevatedListFiles(listOptions);1516 console.log('Files in folder:', fileList);17 return fileList;18 } catch (error) {19 console.error(error);20 // Handle the error21 }22});2324/* 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": false79 * }80 * }81 */
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.
1/********************************************2 * Backend code - download-files-url.web.js *3 *******************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { files } from 'wix-media.v2';7import { elevate } from 'wix-auth';89export const generateDownloadUrl = webMethod(Permissions.Anyone, async (fileIds) => {10 try {11 const elevatedGenerateFilesDownloadUrl = elevate(files.generateFilesDownloadUrl)12 const generatedUrl = await elevatedGenerateFilesDownloadUrl(fileIds);1314 return generatedUrl.downloadUrl;15 } catch (error) {16 console.error(error);17 }18});1920export const listImages = webMethod(Permissions.Anyone, async () => {21 const listOptions = {22 mediaTypes: ["IMAGE"]23 }2425 try {26 const elevatedListFiles = elevate(files.listFiles)27 const images = await elevatedListFiles(listOptions);2829 return images.files;30 } catch (error) {31 console.error(error);32 }33});343536/*************37 * Page code *38 ************/3940import { listImages, generateDownloadUrl } from 'backend/download-files-url.web';4142$w.onReady(() => {43 $w('#downloadFiles').disable();44 getDownloadUrl();45});4647async function getDownloadUrl() {48 const images = await listImages();4950 const imageIds = images.map((image) => {51 return image._id;52 });53 const downloadUrl = await generateDownloadUrl(imageIds);5455 $w('#downloadFiles').link = downloadUrl;56 await $w('#downloadFiles').enable();57}
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.
1/*************************************2 * Backend code - update-file.web.js *3 ************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { files } from 'wix-media.v2';7import { elevate } from 'wix-auth';89export const updateFile = webMethod(Permissions.Anyone, async (fileId, file) => {10 try {11 const elevatedUpdateFileDescriptor = elevate(files.updateFileDescriptor);12 const updatedDescriptor = await elevatedUpdateFileDescriptor(fileId, file);1314 return updatedDescriptor;15 } catch (error) {16 console.error(error);17 }18});1920export const listImages = webMethod(Permissions.Anyone, async () => {21 const listOptions = {22 mediaTypes: ["IMAGE"]23 }2425 try {26 const elevatedListFiles = elevate(files.listFiles)27 const images = await elevatedListFiles(listOptions);2829 return images.files;30 } catch (error) {31 console.error(error);32 }33});343536/*************37 * Page code *38 ************/3940import { updateFile, listImages } from 'backend/update-file.web';4142$w.onReady(async () => {43 await populateImagesDropdown();4445 $w('#update').onClick(async () => {46 const imageId = $w('#imagesDropdown').value;4748 const fileUpdates = {49 displayName: $w('#newDisplayName').value50 }5152 const updatedLocation = await updateFile(imageId, fileUpdates);53 console.log('Display name successfully updated to:', updatedLocation.displayName)54 $w('#updateSuccessMsg').show();55 });56});5758async function populateImagesDropdown() {59 const images = await listImages();60 const dropdownOptions = images.map((image) => {61 return {62 label: image.displayName,63 value: image._id64 };65 });6667 $w('#imagesDropdown').options = dropdownOptions;68};