Search.../

importFile( )

Imports a file to the Media Manager using an external URL.

Description

The importFile() function returns a Promise that resolves to the imported file's descriptor.

This function returns information about the imported file. Importing a file is the method through which you can add files to the Media Manager. Use the parentFolderId parameter to specify which folder you want the file to be imported to. If no folder is specified, the file is imported to the media-root folder.

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

  • 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 importFile(url: string, options: ImportFileOptions): Promise<ImportFileResponse>

importFile Parameters

NAME
TYPE
DESCRIPTION
url
string

Publicly accessible external file URL.

options
Optional
ImportFileOptions

Options to use when importing a single file.

Returns

Return Type:

Promise<
ImportFileResponse
>
NAME
TYPE
DESCRIPTION
file
FileDescriptor

Information about the imported file.

Was this helpful?

Import a file (dashboard page code)

Copy Code
1import { files } from 'wix-media.v2';
2
3/* Sample url value: 'https://example.org/filename.jpg' */
4
5async function myImportFileFunction(url) {
6 try {
7 const importedFile = await files.importFile(url);
8
9 console.log('Imported file successfully:', importedFile);
10 return importedFile;
11 } catch (error) {
12 console.error(error);
13 // Handle the error
14 }
15}
16
17
18/* Promise resolves to:
19 * {
20 * "file": {
21 * "_createdDate": "2023-07-23T09:19:05.000Z",
22 * "_id": "w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg",
23 * "_updatedDate": "2023-07-23T09:19:05.000Z",
24 * "displayName": "image1.jpg",
25 * "hash": "",
26 * "internalTags": [],
27 * "labels": [],
28 * "mediaType": "IMAGE",
29 * "operationStatus": "PENDING",
30 * "parentFolderId": "media-root",
31 * "private": false,
32 * "sizeInBytes": "20167",
33 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
34 * "sourceUrl": "https://example.org/filename.jpg",
35 * "state": "OK",
36 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg",
37 * "url": "https://static.wixstatic.com/media/w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg"
38 * }
39 * }
40 */
Import a file (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 url value: 'https://example.org/filename.jpg'
6
7export const myImportFileFunction = webMethod(Permissions.Anyone, async (url) => {
8 try {
9 const elevatedImportFile = elevate(files.importFile);
10 const importedFile = await elevatedImportFile(url);
11
12 console.log('Imported file successfully:', importedFile);
13 return importedFile;
14 } catch (error) {
15 console.error(error);
16 // Handle the error
17 }
18});
19
20/* Promise resolves to:
21 * {
22 * "file": {
23 * "_createdDate": "2023-07-23T09:19:05.000Z",
24 * "_id": "w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg",
25 * "_updatedDate": "2023-07-23T09:19:05.000Z",
26 * "displayName": "image1.jpg",
27 * "hash": "",
28 * "internalTags": [],
29 * "labels": [],
30 * "mediaType": "IMAGE",
31 * "operationStatus": "PENDING",
32 * "parentFolderId": "media-root",
33 * "private": false,
34 * "sizeInBytes": "20167",
35 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
36 * "sourceUrl": "https://example.org/filename.jpg",
37 * "state": "OK",
38 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg",
39 * "url": "https://static.wixstatic.com/media/w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg"
40 * }
41 * }
42 */
43
Import a file with options

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { files } from 'wix-media.v2';
3import { elevate } from 'wix-auth';
4
5// Sample url value: 'https://example.org/filename.jpg'
6
7export const myImportFileFunction = webMethod(Permissions.Anyone, async (url, importOptions) => {
8 try {
9 const elevatedImportFile = elevate(files.importFile);
10 const importedFile = await elevatedImportFile(url, importOptions);
11
12 console.log('Imported file successfully:', importedFile);
13 return importedFile;
14 } catch (error) {
15 console.error(error);
16 // Handle the error
17 }
18});
19
20/* Promise resolves to:
21 * {
22 * "file": {
23 * "_createdDate": "2023-07-23T09:19:05.000Z",
24 * "_id": "w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg",
25 * "_updatedDate": "2023-07-23T09:19:05.000Z",
26 * "displayName": "image1.jpg",
27 * "hash": "",
28 * "internalTags": [],
29 * "labels": [],
30 * "mediaType": "IMAGE",
31 * "operationStatus": "PENDING",
32 * "parentFolderId": "media-root",
33 * "private": false,
34 * "sizeInBytes": "20167",
35 * "siteId": "3ecba886-4267-11ee-be56-0242ac120002",
36 * "sourceUrl": "https://example.org/filename.jpg",
37 * "state": "OK",
38 * "thumbnailUrl": "https://static.wixstatic.com/media/w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg",
39 * "url": "https://static.wixstatic.com/media/w8ide0_leac4pwk8o11v8725j9ugcbe70x3alc0~mv2.jpg"
40 * }
41 * }
42 */
43