Search.../

importFile( )

Developer Preview

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 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.
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

Copy Code
1import { files } from 'wix-media.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample url value: 'https://example.org/filename.jpg' */
5
6async function myImportFileFunction(url) {
7 try {
8 const elevatedImportFile = elevate(files.importFile);
9 const importedFile = await elevatedImportFile(url);
10
11 console.log('Imported file successfully:', importedFile);
12 return importedFile;
13 } catch (error) {
14 console.error(error);
15 // Handle the error
16 }
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 */
Import a file with options

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