Importing Files

To import files using Import File or Bulk Import File, you need to do one of the following for each file:

  • Specify its MIME type in the mimeType field of the call. For example, mimeType: 'image/jpeg'.

  • Specify 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, specify the file's expected media type in the optional mediaType field of the call. For example, mediaType: 'IMAGE'.

Knowing when a file is ready

When you import a file using the Import File or Bulk Import File, or upload a file, it's not immediately available, meaning you can't manage or use the file straight away. Files can take time to import or upload and be processed. This is true even though the function used to import or upload a file returns a successful response.

To run code when a file finishes processing successfully, listen for the File Ready event. To run a code block when a file's import fails, listen for the File Failed event.

Using externalInfo

The Import File, Bulk Import File, Generate File Upload URL, and Generate File Resumable Upload URL methods have a parameter called externalInfo. Arguments specified in this parameter are included in the event bodies of the File Ready and File Failed events. This is the only place they appear.

Use externalInfo to send information to the events that isn't contained in the file descriptor object.

externalInfo use case

Here is a sample flow to show how you could use externalInfo effectively.

There is a form on a site that sends site visitors a confirmation email with the details they submitted in the form. One of the form fields is an image URL. In the email, we want to send a Wix download URL for the image, not the original image URL. This means we can only send the email when the image file is ready to download.

To implement this, we use the following flow:

  1. When the site visitor submits the form, send the form information to a data collection, getting back the form data's _id.
  2. Call Import File to upload the image to the Media Manager. Specify the externalInfo parameter as follows:
    Copy
  3. Listen for to the File Ready event and implement the following steps to handle it:
    1. Check that the value for externalInfo.origin is formBuilder. We don't want to run this code if media was added from a different source.
    2. Use the form data's _id to retrieve the form details from the CMS.
    3. Generate a download URL for the image.
    4. Send the confirmation email with the download URL and the rest of the form details.
Did this help?