Search.../

bulkImportFiles( )

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.

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

To import files, you need to do one of the following for each file:

  • Pass its MIME type in the mimeType field of the request. For example, mimeType: 'image/jpeg'.
  • Include its extension in either the displayName or url field of the request. For example, displayName: 'Example Image.jpeg or url: https://www.example.com/image.jpeg.
  • Ensure the server hosting the file supports HEAD requests. In these cases the Wix servers can retrieve the MIME type from the hosting server.

    Note: If you want to validate the media type, pass the file's expected media type in the optional mediaType field of the request. For example, mediaType: 'IMAGE'.

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

Copy Code
1import { files } from 'wix-media.v2';
2
3/* Sample importFileRequests value:
4 * [
5 * {
6 * displayName: 'image1',
7 * mediaType: 'IMAGE',
8 * mimeType: 'image/jpeg',
9 * parentFolderId: 'igje5u22nij3qkltzsnol37j3dnthvvh',
10 * url: 'https://www.site1.com/example.jpg'
11 * },
12 * {
13 * displayName: 'vectorArt1',
14 * mediaType: 'VECTOR',
15 * labels : ['label1', 'label2'],
16 * url: 'https://cdn.vectorstock.com/example-vector-art.webp'
17 * },
18 * {
19 * displayName: 'video1',
20 * mediaType: 'VIDEO'
21 * url: 'https://cdn.pixabay.com/example-video.mp4',
22 * }
23 * ]
24 */
25
26async function myBulkImportFilesFunction(importFileRequests) {
27 try {
28 const importedFiles = await files.bulkImportFiles(importFileRequests);
29
30 console.log("Imported files:", importedFiles);
31 return importedFiles;
32 } catch (error) {
33 console.error(error);
34 // Handle the error
35 }
36}
37
38/* Promise resolves to:
39 * {
40 * "files": [
41 * {
42 * "_createdDate": "2023-07-23T12:15:49.000Z",
43 * "_id": "w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg",
44 * "_updatedDate": "2023-07-23T12:15:49.000Z",
45 * "displayName": "image1.jpg",
46 * "hash": "",
47 * "internalTags": [],
48 * "labels": [],
49 * "mediaType": "IMAGE",
50 * "operationStatus": "PENDING",
51 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
52 * "private": false,
53 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
54 * "sizeInBytes": "-1",
55 * "sourceUrl": "https://www.site1.com/example.jpg",
56 * "state": "OK",
57 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg",
58 * "url": "https://static.wixstatic.com/media/w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg"
59 * },
60 * {
61 * "_createdDate": "2023-07-23T12:15:50.000Z",
62 * "_id": "w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg",
63 * "_updatedDate": "2023-07-23T12:15:50.000Z",
64 * "displayName": "vectorArt1.webp.svg",
65 * "hash": "",
66 * "internalTags": [],
67 * "labels": [
68 * "label1",
69 * "label2"
70 * ],
71 * "mediaType": "VECTOR",
72 * "operationStatus": "PENDING",
73 * "parentFolderId": "media-root",
74 * "private": false,
75 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
76 * "sizeInBytes": "20532",
77 * "sourceUrl": "https://cdn.vectorstock.com/example-vector-art.webp",
78 * "state": "OK",
79 * "thumbnailUrl": "https://static.wixstatic.com/shapes/w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg",
80 * "url": "https://static.wixstatic.com/shapes/w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg"
81 * },
82 * {
83 * "_createdDate": "2023-07-23T12:15:50.000Z",
84 * "_id": "w8ide0_f6e8f09e5055432286db483917ba923e",
85 * "_updatedDate": "2023-07-23T12:15:50.000Z",
86 * "displayName": "video1.mp4",
87 * "hash": "",
88 * "internalTags": [],
89 * "labels": [],
90 * "mediaType": "VIDEO",
91 * "operationStatus": "PENDING",
92 * "parentFolderId": "media-root",
93 * "private": false,
94 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
95 * "sizeInBytes": "-1",
96 * "sourceUrl": "https://cdn.pixabay.com/example-video.mp4",
97 * "state": "OK",
98 * "thumbnailUrl": "https://static.wixstatic.com/media/yibv80byru0wei9ficri8fd8o1rghueu.png",
99 * "url": "https://video.wixstatic.com/video/w8ide0_f6e8f09e5055432286db483917ba923e/file"
100 * }
101 * ]
102 * }
103 */
Bulk import files (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 importFileRequests value:
6 * [
7 * {
8 * displayName: 'image1',
9 * mediaType: 'IMAGE',
10 * mimeType: 'image/jpeg',
11 * parentFolderId: 'igje5u22nij3qkltzsnol37j3dnthvvh',
12 * url: 'https://www.site1.com/example.jpg'
13 * },
14 * {
15 * displayName: 'vectorArt1',
16 * mediaType: 'VECTOR',
17 * labels : ['label1', 'label2'],
18 * url: 'https://cdn.vectorstock.com/example-vector-art.webp'
19 * },
20 * {
21 * displayName: 'video1',
22 * mediaType: 'VIDEO'
23 * url: 'https://cdn.pixabay.com/example-video.mp4',
24 * }
25 * ]
26 */
27
28export const myBulkImportFilesFunction = webMethod(Permissions.Anyone, async (importFileRequests) => {
29 try {
30 const elevatedBulkImportFiles = elevate(files.bulkImportFiles)
31 const importedFiles = await elevatedBulkImportFiles(importFileRequests);
32
33 console.log("Imported files:", importedFiles);
34 return importedFiles;
35 } catch (error) {
36 console.error(error);
37 // Handle the error
38 }
39});
40
41/* Promise resolves to:
42 * {
43 * "files": [
44 * {
45 * "_createdDate": "2023-07-23T12:15:49.000Z",
46 * "_id": "w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg",
47 * "_updatedDate": "2023-07-23T12:15:49.000Z",
48 * "displayName": "image1.jpg",
49 * "hash": "",
50 * "internalTags": [],
51 * "labels": [],
52 * "mediaType": "IMAGE",
53 * "operationStatus": "PENDING",
54 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",
55 * "private": false,
56 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
57 * "sizeInBytes": "-1",
58 * "sourceUrl": "https://www.site1.com/example.jpg",
59 * "state": "OK",
60 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg",
61 * "url": "https://static.wixstatic.com/media/w8ide0_h8bpmhc3j3qhgncce0et1nnkghsfozl2~mv2.jpg"
62 * },
63 * {
64 * "_createdDate": "2023-07-23T12:15:50.000Z",
65 * "_id": "w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg",
66 * "_updatedDate": "2023-07-23T12:15:50.000Z",
67 * "displayName": "vectorArt1.webp.svg",
68 * "hash": "",
69 * "internalTags": [],
70 * "labels": [
71 * "label1",
72 * "label2"
73 * ],
74 * "mediaType": "VECTOR",
75 * "operationStatus": "PENDING",
76 * "parentFolderId": "media-root",
77 * "private": false,
78 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
79 * "sizeInBytes": "20532",
80 * "sourceUrl": "https://cdn.vectorstock.com/example-vector-art.webp",
81 * "state": "OK",
82 * "thumbnailUrl": "https://static.wixstatic.com/shapes/w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg",
83 * "url": "https://static.wixstatic.com/shapes/w8ide0_e9xsr959r1ze5aavswj2978r6jt05bwl-tmp.svg"
84 * },
85 * {
86 * "_createdDate": "2023-07-23T12:15:50.000Z",
87 * "_id": "w8ide0_f6e8f09e5055432286db483917ba923e",
88 * "_updatedDate": "2023-07-23T12:15:50.000Z",
89 * "displayName": "video1.mp4",
90 * "hash": "",
91 * "internalTags": [],
92 * "labels": [],
93 * "mediaType": "VIDEO",
94 * "operationStatus": "PENDING",
95 * "parentFolderId": "media-root",
96 * "private": false,
97 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
98 * "sizeInBytes": "-1",
99 * "sourceUrl": "https://cdn.pixabay.com/example-video.mp4",
100 * "state": "OK",
101 * "thumbnailUrl": "https://static.wixstatic.com/media/yibv80byru0wei9ficri8fd8o1rghueu.png",
102 * "url": "https://video.wixstatic.com/video/w8ide0_f6e8f09e5055432286db483917ba923e/file"
103 * }
104 * ]
105 * }
106 */
107
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.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 importFiles = webMethod(Permissions.Anyone, async (importFileRequests) => {
10 try {
11 const elevatedBulkImportFiles = elevate(files.bulkImportFiles)
12 const importedFiles = await elevatedBulkImportFiles(importFileRequests);
13
14 console.log("Imported files:", importedFiles);
15 return importedFiles;
16 } catch (error) {
17 console.error(error);
18 // Handle the error
19 }
20});
21
22
23/*************
24 * Page code *
25 ************/
26
27import { importFiles } from 'backend/bulk-import-files.web';
28
29$w.onReady(() => {
30 const newMedia = [];
31
32 $w('#addMedia').onClick( () => {
33 const newMediaFile = {
34 displayName: $w('#displayName').value,
35 mediaType: $w('#mediaType').value,
36 parentFolderId: $w('#parentFolderId').value,
37 url: $w('#url').value
38 }
39 newMedia.push(newMediaFile);
40
41 // show message to remind user to submit media
42 // after adding all media files for 5 seconds
43 $w('#rememberToUploadMessage').show();
44 setTimeout(() => {
45 $w('#rememberToUploadMessage').hide();
46 }, 5000)
47 });
48
49 $w('#uploadMedia').onClick(async () => {
50 if (newMedia.length === 0) {
51 // if no media has been added then
52 // show message to add media
53 $w('#addMediaMsg').show()
54 } else {
55 await importFiles(newMedia);
56
57 $w('#successfulUploadMsg').show();
58 setTimeout(() => {
59 $w('#successfulUploadMsg').hide();
60 }, 10000);
61 }
62 });
63});
64