bulkImportFile( )
Imports a bulk of files to the Media Manager using external urls.
Description
The bulkImportFile()
function returns a Promise that resolves to an object containing bulk import metadata and an array of imported files' descriptors and metadata.
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 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
orurl
field of the request. For example,displayName: 'Example Image.jpeg
orurl: 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'
.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function bulkImportFile(importFileRequests: Array<ImportFileRequest>, options: BulkImportFileOptions): Promise<BulkImportFileResponse>
bulkImportFile Parameters
NAME
TYPE
DESCRIPTION
Information about the files to import.
Options to include the file descriptor in the response.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
Bulk action metadata.
Items created by bulk action.
Was this helpful?
1import { files } from 'wix-media.v2';23/* 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 */2526async function mybulkImportFileFunction(importFileRequests) {27 try {28 const importedFiles = await files.bulkImportFile(importFileRequests);2930 console.log("Imported files:", importedFiles);31 return importedFiles;32 } catch (error) {33 console.error(error);34 // Handle the error35 }36}3738/* Promise resolves to:39 * {40 * "bulkActionMetadata": {41 * "totalFailures": 0,42 * "totalSuccesses": 3,43 * "undetailedFailures": 044 * },45 * "results": [46 * {47 * "item": {48 * "_createdDate": "2023-12-26T10:22:31.000Z",49 * "_id": "a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",50 * "_updatedDate": "2023-12-26T10:22:31.000Z",51 * "addedBy": {52 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",53 * "identityType": "MEMBER"54 * },55 * "displayName": "image1.jpg",56 * "hash": "",57 * "internalTags": [],58 * "labels": [],59 * "mediaType": "IMAGE",60 * "operationStatus": "PENDING",61 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",62 * "private": false,63 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",64 * "sizeInBytes": "-1",65 * "sourceUrl": "https://www.site1.com/example.jpg",66 * "state": "OK",67 * "thumbnailUrl": "https://static.wixstatic.com/media/a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",68 * "url": "https://static.wixstatic.com/media/a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg"69 * },70 * "itemMetadata": {71 * "_id": "a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",72 * "originalIndex": 0,73 * "success": true74 * }75 * },76 * {77 * "item": {78 * "_createdDate": "2023-12-26T10:22:32.000Z",79 * "_id": "a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",80 * "_updatedDate": "2023-12-26T10:22:32.000Z",81 * "addedBy": {82 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",83 * "identityType": "MEMBER"84 * },85 * "displayName": "vectorArt1.webp.svg",86 * "hash": "",87 * "internalTags": [],88 * "labels": ["label1", "label2"],89 * "mediaType": "VECTOR",90 * "operationStatus": "PENDING",91 * "parentFolderId": "media-root",92 * "private": false,93 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",94 * "sizeInBytes": "-1",95 * "sourceUrl": "https://cdn.vectorstock.com/example-vector-art.webp",96 * "state": "OK",97 * "thumbnailUrl": "https://static.wixstatic.com/shapes/a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",98 * "url": "https://static.wixstatic.com/shapes/a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg"99 * },100 * "itemMetadata": {101 * "_id": "a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",102 * "originalIndex": 1,103 * "success": true104 * }105 * },106 * {107 * "item": {108 * "_createdDate": "2023-12-26T10:22:32.000Z",109 * "_id": "a8a52b_fec67a695b474a729e3a31010afbb0af",110 * "_updatedDate": "2023-12-26T10:22:32.000Z",111 * "addedBy": {112 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",113 * "identityType": "MEMBER"114 * },115 * "displayName": "video1.mp4",116 * "hash": "",117 * "internalTags": [],118 * "labels": [],119 * "mediaType": "VIDEO",120 * "operationStatus": "PENDING",121 * "parentFolderId": "media-root",122 * "private": false,123 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",124 * "sizeInBytes": "-1",125 * "sourceUrl": "https://cdn.pixabay.com/example-video.mp4",126 * "state": "OK",127 * "thumbnailUrl": "https://static.wixstatic.com/media/54ece48fa04e492fa46bd79980105f0d.png",128 * "url": "https://video.wixstatic.com/video/a8a52b_fec67a695b474a729e3a31010afbb0af/file"129 * },130 * "itemMetadata": {131 * "_id": "a8a52b_fec67a695b474a729e3a31010afbb0af",132 * "originalIndex": 2,133 * "success": true134 * }135 * }136 * ]137 * }138 */
1import { Permissions, webMethod } from 'wix-web-module';2import { files } from 'wix-media.v2';3import { elevate } from 'wix-auth';45/* 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 */2728export const mybulkImportFileFunction = webMethod(Permissions.Anyone, async (importFileRequests) => {29 try {30 const elevatedbulkImportFile = elevate(files.bulkImportFile)31 const importedFiles = await elevatedbulkImportFile(importFileRequests);3233 console.log("Imported files:", importedFiles);34 return importedFiles;35 } catch (error) {36 console.error(error);37 // Handle the error38 }39});4041/* Promise resolves to:42 * {43 * "bulkActionMetadata": {44 * "totalFailures": 0,45 * "totalSuccesses": 3,46 * "undetailedFailures": 047 * },48 * "results": [49 * {50 * "item": {51 * "_createdDate": "2023-12-26T10:22:31.000Z",52 * "_id": "a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",53 * "_updatedDate": "2023-12-26T10:22:31.000Z",54 * "addedBy": {55 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",56 * "identityType": "MEMBER"57 * },58 * "displayName": "image1.jpg",59 * "hash": "",60 * "internalTags": [],61 * "labels": [],62 * "mediaType": "IMAGE",63 * "operationStatus": "PENDING",64 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",65 * "private": false,66 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",67 * "sizeInBytes": "-1",68 * "sourceUrl": "https://www.site1.com/example.jpg",69 * "state": "OK",70 * "thumbnailUrl": "https://static.wixstatic.com/media/a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",71 * "url": "https://static.wixstatic.com/media/a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg"72 * },73 * "itemMetadata": {74 * "_id": "a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",75 * "originalIndex": 0,76 * "success": true77 * }78 * },79 * {80 * "item": {81 * "_createdDate": "2023-12-26T10:22:32.000Z",82 * "_id": "a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",83 * "_updatedDate": "2023-12-26T10:22:32.000Z",84 * "addedBy": {85 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",86 * "identityType": "MEMBER"87 * },88 * "displayName": "vectorArt1.webp.svg",89 * "hash": "",90 * "internalTags": [],91 * "labels": ["label1", "label2"],92 * "mediaType": "VECTOR",93 * "operationStatus": "PENDING",94 * "parentFolderId": "media-root",95 * "private": false,96 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",97 * "sizeInBytes": "-1",98 * "sourceUrl": "https://cdn.vectorstock.com/example-vector-art.webp",99 * "state": "OK",100 * "thumbnailUrl": "https://static.wixstatic.com/shapes/a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",101 * "url": "https://static.wixstatic.com/shapes/a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg"102 * },103 * "itemMetadata": {104 * "_id": "a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",105 * "originalIndex": 1,106 * "success": true107 * }108 * },109 * {110 * "item": {111 * "_createdDate": "2023-12-26T10:22:32.000Z",112 * "_id": "a8a52b_fec67a695b474a729e3a31010afbb0af",113 * "_updatedDate": "2023-12-26T10:22:32.000Z",114 * "addedBy": {115 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",116 * "identityType": "MEMBER"117 * },118 * "displayName": "video1.mp4",119 * "hash": "",120 * "internalTags": [],121 * "labels": [],122 * "mediaType": "VIDEO",123 * "operationStatus": "PENDING",124 * "parentFolderId": "media-root",125 * "private": false,126 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",127 * "sizeInBytes": "-1",128 * "sourceUrl": "https://cdn.pixabay.com/example-video.mp4",129 * "state": "OK",130 * "thumbnailUrl": "https://static.wixstatic.com/media/54ece48fa04e492fa46bd79980105f0d.png",131 * "url": "https://video.wixstatic.com/video/a8a52b_fec67a695b474a729e3a31010afbb0af/file"132 * },133 * "itemMetadata": {134 * "_id": "a8a52b_fec67a695b474a729e3a31010afbb0af",135 * "originalIndex": 2,136 * "success": true137 * }138 * }139 * ]140 * }141 */142
1import { Permissions, webMethod } from 'wix-web-module';2import { files } from 'wix-media.v2';3import { elevate } from 'wix-auth';45/* 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 */2728export const mybulkImportFileFunction = webMethod(Permissions.Anyone, async (importFileRequests, options) => {29 try {30 const elevatedbulkImportFile = elevate(files.bulkImportFile)31 const importedFiles = await elevatedbulkImportFile(importFileRequests, options);3233 console.log("Imported files:", importedFiles);34 return importedFiles;35 } catch (error) {36 console.error(error);37 // Handle the error38 }39});4041/* Promise resolves to:42 * {43 * "bulkActionMetadata": {44 * "totalFailures": 0,45 * "totalSuccesses": 3,46 * "undetailedFailures": 047 * },48 * "results": [49 * {50 * "item": {51 * "_createdDate": "2023-12-26T10:22:31.000Z",52 * "_id": "a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",53 * "_updatedDate": "2023-12-26T10:22:31.000Z",54 * "addedBy": {55 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",56 * "identityType": "MEMBER"57 * },58 * "displayName": "image1.jpg",59 * "hash": "",60 * "internalTags": [],61 * "labels": [],62 * "mediaType": "IMAGE",63 * "operationStatus": "PENDING",64 * "parentFolderId": "igje5u22nij3qkltzsnol37j3dnthvvh",65 * "private": false,66 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",67 * "sizeInBytes": "-1",68 * "sourceUrl": "https://www.site1.com/example.jpg",69 * "state": "OK",70 * "thumbnailUrl": "https://static.wixstatic.com/media/a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",71 * "url": "https://static.wixstatic.com/media/a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg"72 * },73 * "itemMetadata": {74 * "_id": "a8a52b_f8c86723942347488316f1ab35119411~mv2.jpg",75 * "originalIndex": 0,76 * "success": true77 * }78 * },79 * {80 * "item": {81 * "_createdDate": "2023-12-26T10:22:32.000Z",82 * "_id": "a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",83 * "_updatedDate": "2023-12-26T10:22:32.000Z",84 * "addedBy": {85 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",86 * "identityType": "MEMBER"87 * },88 * "displayName": "vectorArt1.webp.svg",89 * "hash": "",90 * "internalTags": [],91 * "labels": ["label1", "label2"],92 * "mediaType": "VECTOR",93 * "operationStatus": "PENDING",94 * "parentFolderId": "media-root",95 * "private": false,96 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",97 * "sizeInBytes": "-1",98 * "sourceUrl": "https://cdn.vectorstock.com/example-vector-art.webp",99 * "state": "OK",100 * "thumbnailUrl": "https://static.wixstatic.com/shapes/a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",101 * "url": "https://static.wixstatic.com/shapes/a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg"102 * },103 * "itemMetadata": {104 * "_id": "a8a52b_43eb6841371f4d738ddfcb7fa2bb62be-tmp.svg",105 * "originalIndex": 1,106 * "success": true107 * }108 * },109 * {110 * "item": {111 * "_createdDate": "2023-12-26T10:22:32.000Z",112 * "_id": "a8a52b_fec67a695b474a729e3a31010afbb0af",113 * "_updatedDate": "2023-12-26T10:22:32.000Z",114 * "addedBy": {115 * "identityId": "a8a52bb9-bac0-4bba-a55c-0611b5cdaa16",116 * "identityType": "MEMBER"117 * },118 * "displayName": "video1.mp4",119 * "hash": "",120 * "internalTags": [],121 * "labels": [],122 * "mediaType": "VIDEO",123 * "operationStatus": "PENDING",124 * "parentFolderId": "media-root",125 * "private": false,126 * "siteId": "49dcb500-1081-47b0-b13b-368b9bed8fa9",127 * "sizeInBytes": "-1",128 * "sourceUrl": "https://cdn.pixabay.com/example-video.mp4",129 * "state": "OK",130 * "thumbnailUrl": "https://static.wixstatic.com/media/54ece48fa04e492fa46bd79980105f0d.png",131 * "url": "https://video.wixstatic.com/video/a8a52b_fec67a695b474a729e3a31010afbb0af/file"132 * },133 * "itemMetadata": {134 * "_id": "a8a52b_fec67a695b474a729e3a31010afbb0af",135 * "originalIndex": 2,136 * "success": true137 * }138 * }139 * ]140 * }141 */142
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 bulkImportFile()
function.
1/****************************************2 * Backend code - bulk-import-files.web.js *3 ***************************************/45import { Permissions, webMethod } from 'wix-web-module';6import { files } from 'wix-media.v2';7import { elevate } from 'wix-auth';89export const importFiles = webMethod(Permissions.Anyone, async (importFileRequests) => {10 try {11 const elevatedBulkImportFile = elevate(files.bulkImportFile)12 const importedFiles = await elevatedBulkImportFile(importFileRequests);1314 console.log("Imported files:", importedFiles);15 return importedFiles;16 } catch (error) {17 console.error(error);18 // Handle the error19 }20});212223/*************24 * Page code *25 ************/2627import { importFiles } from 'backend/bulk-import-files.web';2829$w.onReady(() => {30 const newMedia = [];3132 $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').value38 }39 newMedia.push(newMediaFile);4041 // show message to remind user to submit media42 // after adding all media files for 5 seconds43 $w('#rememberToUploadMessage').show();44 setTimeout(() => {45 $w('#rememberToUploadMessage').hide();46 }, 5000)47 });4849 $w('#uploadMedia').onClick(async () => {50 if (newMedia.length === 0) {51 // if no media has been added then52 // show message to add media53 $w('#addMediaMsg').show()54 } else {55 await importFiles(newMedia);5657 $w('#successfulUploadMsg').show();58 setTimeout(() => {59 $w('#successfulUploadMsg').hide();60 }, 10000);61 }62 });63});64