Upload a File with a Generated File Upload URL

To upload a file to a site's Media Manager, you can call Generate File Upload URL. This call returns a signed uploadUrl that you can then use to upload a file.

Note: For files with public URLs, you can call Import File instead of calling the Generate File Upload URL.

Upload flow

The code for generating an upload URL and sending it an upload request varies depending on your context. For example, the code for a dashboard page of a self-hosted app differs slightly from the code for a headless site.

However, in all scenarios, you need to perform the following steps:

  1. Get the file's content as a binary stream.
  2. Get the name of the file.
  3. Get the MIME type of the file.
  4. Generate an upload URL using the Generate File Upload URL method.
  5. If you didn't provide the file name in the Generate File Upload URL call, append it to the generated upload URL as the filename query parameter.
  6. Call the generated upload URL to upload the file.

Upload URL call

After generating an uploadUrl, send a request to it using the following information:

  • HTTP method: PUT.
  • Headers: "Content-Type" with the appropriate MIME type, such as "image/jpeg" or "video/mp4".
  • Query parameters: filename with the name of the file, including the extension. The filename parameter is optional if fileName was already provided in the Generate File Upload URL call.
  • Body: The file content as a binary stream.

Note: You need to determine the file's MIME type before uploading. For example, in Node.js you can use the mime-types library or the file-type package. Other languages and platforms have equivalent libraries for detecting MIME types.

Upload URL response

When the upload is successful, Wix returns an uploadUrl with information about the file being uploaded.

Uploading files takes time. Receiving a successful response from a call to the uploadUrl doesn't mean that the upload is complete. To run code when the upload finishes, listen for the File Ready and File Failed events. Learn more about knowing when a file is ready.

Example

The following example code shows the calls to generate an upload URL and use it to upload an image file. How you make those calls depends on the context in which you're writing that code.

You also need to write code that performs the other steps in the upload flow to retrieve the information you need to provide when making the calls shown below.

Generate an upload URL

To generate an upload URL, you need to send a POST request to Generate File Upload URL, specifying the MIME type of the file.

Copy

The response contains a signed uploadUrl.

Use a generated URL to upload a file

After generating an uploadUrl, you can upload the file by sending a PUT request to the generated URL.

Copy

Example response

A call to a generated uploadUrl returns a response structured as follows:

Copy

See also

Did this help?