Search.../

startUpload( )

Deprecated. This function will continue to work, but a newer version is available. Use the uploadFiles() function instead.

Description

Uploads the file that the site visitor has chosen.

The startUpload() function triggers the upload of the file in the upload button's value property.

Migration Instructions

If this function is already in your code, it will continue to work. To stay compatible with future changes, migrate to the new uploadFiles() function:

  1. Update your code based on the new code example.
  2. Test your changes to make sure your code behaves as expected.

Note the following differences between the new uploadFiles() function and the deprecated startUpload() function:

  • uploadFiles() triggers the upload of multiple files instead of a single file.
  • uploadFiles() returns an array of UploadedFile objects instead of a single UploadFile object.
  • The new UploadedFile object contains different properties than the deprecated UploadFile object.

Note the following returns mapped to the uploadFiles() function returns as follows:

  • url and mediaId --> fileUrl
  • title --> originalFileName

Syntax

function startUpload(): Promise<UploadFile>

startUpload Parameters

This function does not take any parameters.

Returns

Fulfilled - Resolves when the file upload is completed and returns an UploadFile object. Rejected - Rejects if the file upload fails and returns an UploadError object, which contains the error code and description.

Return Type:

Promise<UploadFile>
NAME
TYPE
DESCRIPTION
url
string

Wix URL of the successfully uploaded file.

mediaId
string

Wix media ID of the uploaded file.

title
string

Title of the uploaded file.

width
number

Width of an uploaded image or video file.

height
number

Height of an uploaded image or video file.

duration
number

Duration (in milliseconds) of an uploaded audio file.

Was this helpful?

Start a file upload

This example uses a deprecated function.

Copy Code
1
2$w("#myUploadButton").startUpload()
3 .then( (uploadedFile) => {
4 let url = uploadedFile.url; // "wix:image://v1/68d3a9_1de7529c444b4c9eb38401f8efe0cad2.jpg/flowers.jpg#originWidth=1970&originHeight=1120"
5 } )
6 .catch( (uploadError) => {
7 let errCode = uploadError.errorCode; // 7751
8 let errDesc = uploadError.errorDescription; // "Error description"
9 } );
10
11
12
13
Typical file upload scenario

This example uses a deprecated function.

This example uploads multiple files when the site visitor clicks a button. First it sets the type of file a site visitor can choose (in this case, an image). Then the example checks to see if the site visitor chose files using the upload button. If files were chosen, it triggers the uploadFiles() function and logs the status of the file upload when it completes successfully or with an error.

Copy Code
1
2$w("#myUploadButton").fileType = "Image"; // Site visitor can choose an image
3$w('#myButton').onClick( () => {
4 if ($w("#myUploadButton").value.length > 0) { // Site visitor chose a file
5 console.log("Uploading " + $w("#myUploadButton").value[0].name + "- Please wait.");
6 $w("#myUploadButton").startUpload()
7 .then( (uploadedFile) => {
8 console.log("Upload successful. File is available here:");
9 console.log(uploadedFile.url);
10 } )
11 .catch( (uploadError) => {
12 console.log("File upload error: " + uploadError.errorCode);
13 console.log(uploadError.errorDescription);
14 } );
15 }
16 else { // Site visitor clicked button but didn't choose a file
17 console.log("Please choose a file to upload.")
18 }
19} );
20