Search.../

bulkImportFiles( )

Developer Preview

Deprecated. This function has been replaced with bulkImportFile(), and will be removed on March 31, 2024.

Description

The bulkImportFiles() function returns a Promise that resolves to an array of the imported files' descriptors.

Imports a bulk of files to the Media Manager using external urls.

Returns information about the imported files. Use the parentFolderId parameter to specify in which folder you want each file to be imported. If no folder is specified, the file is imported to the media-root folder.

To import a file, you need to pass one of the following:

  • The file's MIME type in the mimeType parameter of the request. For example, 'image/png'.
  • The file's name and extension. For example, 'my-image.png'.
  • If you don't know the file's extension or MIME type, its media type in the mediaType parameter of the request. For example, 'IMAGE'. Note that this option only works if the server hosting the media allows a 'HEAD' request.

Note: The media property isn't returned in the files response object.

Admin Method

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

Syntax

function bulkImportFiles(importFileRequests: Array<ImportFileRequest>): Promise<BulkImportFilesResponse>

bulkImportFiles Parameters

NAME
TYPE
DESCRIPTION
importFileRequests
Array<
ImportFileRequest
>

Information about the files to import.

Returns

Return Type:

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

Information about the imported files.

Was this helpful?

Bulk import files

Copy Code
1import { files } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample importFileRequests value:
5 * [
6 * {
7 * displayName: 'image1',
8 * mediaType: 'IMAGE',
9 * mimeType: 'image/jpeg',
10 * parentFolderId: 'igje5u22nij3qkltzsnol37j3dnthvvh',
11 * url: 'https://www.site1.com/example.jpg'
12 * },
13 * {
14 * displayName: 'vectorArt1',
15 * mediaType: 'VECTOR',
16 * labels : ['label1', 'label2'],
17 * url: 'https://cdn.vectorstock.com/example-vector-art.webp'
18 * },
19 * {
20 * displayName: 'video1',
21 * mediaType: 'VIDEO'
22 * url: 'https://cdn.pixabay.com/example-video.mp4',
23 * }
24 * ]
25 */
26
27async function myBulkImportFilesFunction(importFileRequests) {
28 try {
29 const elevatedBulkImportFiles = elevate(files.bulkImportFiles)
30 const importedFiles = await elevatedBulkImportFiles(importFileRequests);
31
32 console.log("Imported files:", importedFiles);
33 return importedFiles;
34 } catch (error) {
35 console.error(error);
36 // Handle the error
37 }
38}
39
40/* Promise resolves to:
41 * {
42 * "files": [
43 * {
44 * "_createdDate": "2023-07-23T12:15:49.000Z",
45 * "_id": "w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg",
46 * "_updatedDate": "2023-07-23T12:15:49.000Z",
47 * "displayName": "image1.jpg",
48 * "hash": "",
49 * "internalTags": [],
50 * "labels": [],
51 * "mediaType": "IMAGE",
52 * "operationStatus": "PENDING",
53 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
54 * "private": false,
55 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
56 * "sizeInBytes": "-1",
57 * "sourceUrl": "https://www.site1.com/example.jpg",
58 * "state": "OK",
59 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg",
60 * "url": "https://static.wixstatic.com/media/w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg"
61 * },
62 * {
63 * "_createdDate": "2023-07-23T12:15:50.000Z",
64 * "_id": "w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg",
65 * "_updatedDate": "2023-07-23T12:15:50.000Z",
66 * "displayName": "vectorArt1.webp.svg",
67 * "hash": "",
68 * "internalTags": [],
69 * "labels": [
70 * "label1",
71 * "label2"
72 * ],
73 * "mediaType": "VECTOR",
74 * "operationStatus": "PENDING",
75 * "parentFolderId": "media-root",
76 * "private": false,
77 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
78 * "sizeInBytes": "20532",
79 * "sourceUrl": "https://cdn.vectorstock.com/example-vector-art.webp",
80 * "state": "OK",
81 * "thumbnailUrl": "https://static.wixstatic.com/shapes/w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg",
82 * "url": "https://static.wixstatic.com/shapes/w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg"
83 * },
84 * {
85 * "_createdDate": "2023-07-23T12:15:50.000Z",
86 * "_id": "w8ide0_f6e8f09e5055432286db483917ba923e",
87 * "_updatedDate": "2023-07-23T12:15:50.000Z",
88 * "displayName": "video1.mp4",
89 * "hash": "",
90 * "internalTags": [],
91 * "labels": [],
92 * "mediaType": "VIDEO",
93 * "operationStatus": "PENDING",
94 * "parentFolderId": "media-root",
95 * "private": false,
96 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
97 * "sizeInBytes": "-1",
98 * "sourceUrl": "https://cdn.pixabay.com/example-video.mp4",
99 * "state": "OK",
100 * "thumbnailUrl": "https://static.wixstatic.com/media/yibv80byru0wei9ficri8fd8o1rghueu.png",
101 * "url": "https://video.wixstatic.com/video/w8ide0_f6e8f09e5055432286db483917ba923e/file"
102 * }
103 * ]
104 * }
105 */
Bulk import files from visitor input into a form

This code shows a page where the visitor adds basic media file data to a form and then adds it to the newMedia array. When the visitor clicks the upload button, all added media files are uploaded using the bulkImportFiles() function.

Copy Code
1/****************************************
2 * Backend code - bulk-import-files.jsw *
3 ***************************************/
4
5import { files } from 'wix-media.v2';
6import { elevate } from 'wix-auth';
7
8export async function importFiles(importFileRequests) {
9 try {
10 const elevatedBulkImportFiles = elevate(files.bulkImportFiles)
11 const importedFiles = await elevatedBulkImportFiles(importFileRequests);
12
13 console.log("Imported files:", importedFiles);
14 return importedFiles;
15 } catch (error) {
16 console.error(error);
17 // Handle the error
18 }
19}
20
21
22/*************
23 * Page code *
24 ************/
25
26import { importFiles } from 'backend/bulk-import-files';
27
28$w.onReady(() => {
29 const newMedia = [];
30
31 $w('#addMedia').onClick( () => {
32 const newMediaFile = {
33 displayName: $w('#displayName').value,
34 mediaType: $w('#mediaType').value,
35 parentFolderId: $w('#parentFolderId').value,
36 url: $w('#url').value
37 }
38 newMedia.push(newMediaFile);
39
40 // show message to remind user to submit media
41 // after adding all media files for 5 seconds
42 $w('#rememberToUploadMessage').show();
43 setTimeout(() => {
44 $w('#rememberToUploadMessage').hide();
45 }, 5000)
46 });
47
48 $w('#uploadMedia').onClick(async () => {
49 if (newMedia.length === 0) {
50 // if no media has been added then
51 // show message to add media
52 $w('#addMediaMsg').show()
53 } else {
54 await importFiles(newMedia);
55
56 $w('#successfulUploadMsg').show();
57 setTimeout(() => {
58 $w('#successfulUploadMsg').hide();
59 }, 10000);
60 }
61 });
62});