Velo Tutorial: Using the Upload Button with Code
File Upload Scenario
Upload Example
Prerequisites
Add Page Elements
- An upload button for choosing a file. The upload buttons are found in the User Input section of the Add menu.
- A regular button to trigger the upload code.
- A text element to display messages to the site visitor.
- An image element. Any image is fine. We're only using it as a placeholder for the uploaded image.
Feel free to change the design and text of any of the elements we've just added.
The complete form should look something like this:

Wire Upload Code
1export function button1_click(event) {
2 if($w("#uploadButton1").value.length > 0) {
3 $w("#text1").text = "Uploading " + $w("#uploadButton1").value[0].name;
4 $w("#uploadButton1").uploadFiles()
5 .then( (uploadedFiles) => {
6 $w("#text1").text = "Upload successful";
7 $w("#image1").src = uploadedFiles[0].fileUrl;
8 })
9 .catch( (uploadError) => {
10 $w("#text1").text = "File upload error";
11 console.log("File upload error: " + uploadError.errorCode);
12 console.log(uploadError.errorDescription);
13 });
14 }
15 else {
16 $w("#text1").text = "Please choose a file to upload.";
17 }
18}
First, on line 2, we check to see if the site visitor has chosen a file. We did not specify a file type, so "image" is assumed.
if($w("#uploadButton1").value.length > 0) {
The value
property of the upload button returns an array of File
objects, each representing a file the site visitor has chosen to upload. So we check its length
to see if this array has at least one item in it, meaning the site visitor has chosen at least one file.
If the array returned by value
is empty, we move to the else
on lines 15-17 to display a message to the site visitor in the text element.
else {
$w("#text1").text = "Please choose a file to upload.";
}
That ends the upload attempt. The site visitor can then choose a file and start the upload again.
If the array has a File
element in it, meaning the site visitor chose a file to upload, we continue with the upload process.
On line 3, we start the upload process by displaying a message to the site visitor that we are uploading the chosen file.
$w("#text1").text = "Uploading " + $w("#uploadButton1").value[0].name;
Next, on line 4, we call the upload button's uploadFiles()
function, which performs the actual upload.
$w("#uploadButton1").uploadFiles()
It returns a Promise that resolves to an array of UploadedFile
objects with information about the uploaded files if the upload was successful or rejects to an UploadError
object with information about the error that occurred if the upload was not successful.
If the upload is successful, the Promise continues with the then()
on lines 5-8.
.then( (uploadedFiles) => {
$w("#text1").text = "Upload successful";
$w("#image1").src = uploadedFiles[0].fileUrl;
})
Here we display a message to the site visitor saying that the upload was successful and we display the uploaded image. We display the image by getting the fileUrl
property of the first UploadedFile
object in the array, which returns the Wix URL where the uploaded file is stored.
If the upload is unsuccessful, the Promise rejects with the catch()
on lines 9-13.
.catch( (uploadError) => {
$w("#text1").text = "File upload error";
console.log("File upload error: " + uploadError.errorCode);
console.log(uploadError.errorDescription);
});
Here we display a message to the site visitor saying that the upload was not successful. We also log the error information from the uploadError
object using its errorCode
and errorDescription
properties.
Testing
Next, let's use the upload button to choose a file and then click the Start Upload button. We should see the status line change to Uploading some_file.png and then after a few moments change again to Upload successful. The image we uploaded should also be displayed on the page.
Finally, let's return to the Editor and find the uploaded file. Select the upload button and click Manage Upload Button. In the Upload Button Settings panel, click Go to File, which opens the images that have been received by your site. There you should see the image you just uploaded.