Preview image upload before submitting to database

I’ve created a user form that will eventually be restricted to only specific users so that event details can be added on the go including images and downloadable content (i.e. event flyer).
Everything seems to be working pretty well but I want to have the user able to preview the content before submitting to the database. See the image below.

I’ve looked at a few different options (I got it working the way I wanted, kind of, thanks to some other forum posts and example codes) but I would like the uploaded image/s to be added to the database rather than the visitor uploads section of the media manager so that the user doesn’t run into any “No File To Upload” error messages.

Here’s the page code I have so far:

import wixLocation from 'wix-location';
import wixData from 'wix-data';

//Content previews
$w.onReady(function () {
    $w("#eventsDataset").onReady(() => {
        updateInfo();
    });

})

$w.onReady(function () {
    $w('#previewButton').onClick((event) => {
        updateInfo();
    })
});

function updateInfo() {
    console.log('Refreshed Data');
 if ($w('#eventTitleInput').value === '') {
        $w('#eventTitle').text = 'Add an Event Title'
    } else {
        $w('#eventTitle').text = $w('#eventTitleInput').value;
    }
 if ($w('#eventDescriptionInput').value === '') {
        $w('#eventDescription').text = 'Add an Event Description'
    } else {
        $w('#eventDescription').text = $w('#eventDescriptionInput').value;
    }
 if ($w('#facebookEventUrlInput').value === '') {} else {
        $w('#infoButton').link = $w('#facebookEventUrlInput').value;
    }
 if ($w('#eventDateInput').value === '') {
        $w('#datePicker1').value = Date()
    } else {
        $w('#datePicker1').value = $w('#eventDateInput').value;
    }
 if ($w('#eventLocationInput').value === '') {
        $w('#eventLocation').text = 'Add an Event Location'
    } else {
        $w('#eventLocation').text = $w('#eventLocationInput').value;
    }
}

//Image upload and previews
$w.onReady(function () {
    $w("#eventFlyerImageUpload").onChange(() => {
 if ($w("#eventFlyerImageUpload").value.length > 0) { // user chose a file
            console.log(`Uploading '${$w("#eventFlyerImageUpload").value[0].name}'`);
            $w('#fileUploadRenameError').hide();
            $w("#eventFlyerImageUpload").startUpload()
                .then((uploadedFile) => {
                    console.log("Upload successful. File is available here:");
                    console.log(uploadedFile.url);
                    $w('#getFlyerButton').onClick((event1) => {
 let url = uploadedFile.url.split("/")[3];
 let filename = 'Event Flyer Preview';
                        wixLocation.to(`https://static.wixstatic.com/media/${url}?dn=${filename}`);
                    })
                    wixData // need to use save/update method.
                })
                .then(() => {
                    console.log('done Save/Update')
                })
                .catch((uploadError) => {
                    console.log(`File upload error: ${uploadError.errorCode}`);
 if (uploadError.errorDescription === 'No File To Upload') {
                        $w("#eventBannerImageUpload").reset();
                        $w('#fileUploadRenameError').show();
                    }
                    console.log(uploadError.errorDescription);
                });
        } else { // user clicked button but didn't chose a file
            console.log("Please choose a file to upload.")
            $w('#fileUploadRenameError').hide();
        }
    });
});

$w.onReady(function () {
    $w("#eventBannerImageUpload").onChange(() => {
 if ($w("#eventBannerImageUpload").value.length > 0) { // user chose a file
            console.log(`Uploading '${$w("#eventBannerImageUpload").value[0].name}'`);
            $w('#fileUploadRenameError').hide();
            $w("#eventBannerImageUpload").startUpload()
                .then((uploadedFile) => {
                    console.log("Upload successful. File is available here:");
                    console.log(uploadedFile.url);
                    $w('#previewButton').onClick((event) => {
                        $w('#bannerImage').src = uploadedFile.url;
                    })
                    wixData // need to use save/update method.
                })
                .then(() => {
                    console.log('done Save/Update')
                })
                .catch((uploadError) => {
                    console.log(`File upload error: ${uploadError.errorCode}`);
 if (uploadError.errorDescription === 'No File To Upload') {
                        $w("#eventBannerImageUpload").reset();
                        $w('#fileUploadRenameError').show();
                    }
                    console.log(uploadError.errorDescription);
                });
        } else { // user clicked button but didn't chose a file
            console.log("Please choose a file to upload.")
            $w('#fileUploadRenameError').hide();
        }
    });
});

Any ideas?

1 Like

@yisrael-wix Any suggestions on how to achieve this?

Following

Try this previous forum post and add the uploaded file url into the code so that they can view it.
https://www.wix.com/code/home/forum/community-discussion/simple-solution-for-upload-progress-of-image

Hi @givemeawhisky
Thanks for the suggestion. I did already experiment with this code but as I said, I would like the uploaded image/s to be added to the database rather than the visitor uploads section of the media manager so that the user doesn’t run into any “No File To Upload” error messages.

I would like to see an answer to this too.
UploadButton show have a preview method.

$w(‘uploadButton1’).preview() should check for validity, and then populate the container with the image.
Currently the only way I’ve found to do this is to actually upload the file, then show() an Image with that… but then what happens is that a user will “preview” multiple images, and so the Visitor Uploads section will have multiple uploads that are essentially dead

Wix already do a tutorial for previewing the image etc, see here for more info.
https://support.wix.com/en/article/wix-code-using-the-upload-button-with-code

As for saving it to a dataset itself instead of having to go to the upload button to get the image files etc, then simply add the save command to your page as stated in the support page for the upload button itself.
https://support.wix.com/en/article/adding-and-setting-up-an-upload-button

  • Tips:

  • You can also let users download a file in your collection (not images) by selecting an element that you can add a link to (buttons, tables, images, galleries). Then click the Connect to Data icon and select the field in your collection where you stored your file. When a user clicks the linked element, the file downloads automatically.

  • You need to add a submit button to your page to save user data to your collection.

Or simply add the save function to your own code instead.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#save

Also, the original poster here would need to be using their own custom made events page as they will not be able to do it any other way.

If they have tried to use the Wix Events app with their used code, then it will not work as the Wix Events app is not fully integrated into Wix Corvid as of yet.

This is the only thing that you can do so far with Wix Events in Corvid.
https://support.wix.com/en/article/corvid-wix-events-events-collection-fields
Permissions
The Events collection has the following permissions :

  • Read: Anyone

  • Create: None

  • Update: None

  • Delete: None
    You cannot change the Events collection’s permissions.

https://support.wix.com/en/article/creating-a-custom-events-gallery
https://support.wix.com/en/article/creating-a-custom-event-details-page

.then((uploadedFile) => {
$w(‘#getFlyerButton’).onClick((event1) =>
{ let url = uploadedFile.url;
let filename = ‘Event Flyer Preview’;
});
let toInsert = {
“databse_property” : url ;
}
wixData.insert( “database_name” , toInsert);

});