getUploadUrl( )
Gets an upload URL for uploading a file to the media manager.
Description
The getUploadUrl()
function returns a Promise that resolves to an object
containing an upload URL.
Use the getUploadUrl()
function to allow an external client to upload a single file to your
site's Media Manager.
Note: The upload URL is valid for a single file upload and expires after 1 day. The
getUploadUrl()
function must be called for each file that you want to upload.
The external client uploads a file by sending a POST or PUT request to the
URL returned by getUploadUrl()
using multipart/form-data encoding and
sending the following data:
javascript | Copy Code{"upload_url": <uploadUrl>,"file": {"value": <contentStream>,"options": {"filename": <fileName>,"contentType": <contentType>}}};
upload_url
: The returned upload URL.file
: An object containing:value
: The file content as a multi-part value.options
: An object containing:filename
: The name of the file as it will appear in the Media Manager.contentType
: The content type of the file.
The POST or PUT request returns the following:
javascript | Copy Code[{"parent_folder_id": <folder id>,"hash": <file hash>,"tags": [],"file_name": <internal file name>,"refs": [],"labels": [] <image recognition labels of the image content>,"site_id": <site id>,"height": <image height>,"original_file_name": <the original file name>,"file_size": <file size>,"width": <image width>,"media_type": "picture","mime_type": "image/jpeg"}]
To get a Wix image URL that can be stored in a collection or displayed in an image element or gallery from the above object, use the following expression:
Copy Codelet wixImageUrl = `wix:image://v1/${response[0].file_name}/${response[0].original_file_name}#originWidth=${response[0].width}&originHeight=${response[0].height}`;
Syntax
function getUploadUrl(path: string, options: UploadOptions): UploadUrl
getUploadUrl Parameters
NAME
TYPE
DESCRIPTION
The path within the Media Manager where the file will be uploaded. If the path doesn't yet exist, the missing folders will be created.
Options to use when uploading the file.
Returns
Fulfilled - The file's URL.
Return Type:
NAME
TYPE
DESCRIPTION
The URL used for sending a POST or PUT request to upload a file to the Media Manager.
Deprecated. Use the uploadUrl
property instead.
The token to use with the file POST.
Note: The uploadToken
property returns empty, as the token is now included in the upload URL. The uploadToken
property will continue to work, but we recommend that you use the updated uploadUrl
parameter instead.
Was this helpful?
This example demonstrates how to generate a URL that an external application can use to upload a file to your site's Media Manager.
1import { Permissions, webMethod } from 'wix-web-module';2import { mediaManager } from 'wix-media-backend';34export const getUploadUrl = webMethod(Permissions.Anyone, () => {5 return mediaManager.getUploadUrl(6 "/myUploadFolder/subfolder",7 {8 "mediaOptions": {9 "mimeType": "image/jpeg",10 "mediaType": "image"11 },12 "metadataOptions": {13 "isPrivate": false,14 "isVisitorUpload": false,15 "context": {16 "someKey1": "someValue1",17 "someKey2": "someValue2"18 }19 }20 }21 );22});
uploadUrl
to send a PUT request to upload an imageThis example demonstrates how an upload URL generated in the first code example could be used by an external application to upload an image file to the Media Manager using a PUT request. It also creates a Wix image URL from the response.
1/********************************2 * External Node.js application *3********************************/45import axios from 'axios';67async function myUploadImageFunction(uploadUrl, contentStream, fileName, contentType) {8 const response = await axios.put(uploadUrl, contentStream, {9 headers: {10 'Content-Type': contentType11 },12 params: {13 filename: fileName14 }15 });1617 return `wix:image://v1/${response[0].file_name}/${response[0].original_file_name}#originWidth=${response[0].width}&originHeight=${response[0].height}`;18}
This example demonstrates how an upload URL generated in the first code example could be used by an external application to upload an image file to the Media Manager using a POST request.
1/********************************2 * External Node.js application *3********************************/45import axios from 'axios';6import FormData from 'form-data';78async function myUploadImageFunction(uploadUrl: string, fileName: string, content: Buffer | fs.ReadStream | string) {9 const formData = new FormData({});10 formData.append('file', content, {11 filename: fileName,12 contentType: 'image/jpeg',13 });1415 const uploadResponse = await axios.post(16 uploadUrl,17 formData,18 {19 headers: formData.getHeaders()20 });2122 return uploadResponse;23}