How can I control users' (non-members) upload count per session?

Hello-
Basically I’m trying to limit the number of images that a single user can upload in a single session (let’s say 2). I know I need to use session from wix-storage, but as I’m not heavy on coding, I couldn’t make it right.
Currently an upload to a ‘Wix Pro Gallery’ is being made via an ‘upload button’ and a ‘button’ which inserts the single uploaded image into a Dataset. Any idea how the combination between session and ’ databaseNumberOfImages ’ works within my code?
After researching in the forum, I believe that I need to add this function code (or similar) into my code:

This is the code which needs I need help in editing:
Import { session } from ‘wix-storage’;
session.setItem(“key”, “value”)
let databaseNumberOfImages = 2;
if (items.length >= databaseNumberOfImages) {
$w(" #uploadButton ").disable();

Here’s the part of my (working) code, which handles the upload, and which I’d need to add the new code above into:
import wixData from ‘wix-data’;
export function checkbox1_change(event, $w) { //ensures T&Cs box is ticked, otherwise ‘Upload’ button is disabled.
if ($w(“#checkbox1”).checked=== true ){
$w(“#button1”).enable();
$w(“#dataset1”).setFieldValue(“agreedTCs”, true );
$w(“#dataset1”).save();
}
if ($w(“#checkbox1”).checked=== false ){
$w(“#button1”).disable();
}
}
export function button1_click_1(event) { //Upload button to select a photo
if ($w(“#uploadButton1”).value.length > 0) {
$w(“#text19”).text = Uploading ${$w("#uploadButton1").value[0].name};
setTimeout( function () {
// $w(“#text19”).text = “Almost there… "
});
$w(”#uploadButton1").startUpload()
.then( (uploadedFile) => {
$w(“#text19”).text = “Upload successful”;
let toInsert = {
“MyPhoto”: uploadedFile.url
};

wixData.insert(“DD”, toInsert)
//Inserting the uploaded photo into the DB
.then( (results) => {
let item = results;
}).then(()=>{
$w(“#dataset1”).refresh();

});
})
. catch ( (uploadError) => {
$w(“#text19”).text = “File upload error”;
console.log(File upload error: ${uploadError.errorCode});
console.log(uploadError.errorDescription);
} );
}

}

Thanks for you help all.

(First, please edit your post subject so it’ll include the thread topic.)
Second, are you sure you want to use “session”? Can you described what you’re trying to achieve?

Hi @jonatandor35
I’d like to limit the number of images that a single user can upload in a single session. The upload is being made into a Pro Gallery. The bottom line here is how can I control users’ (non-members) upload count per session. I’m collecting pieces of code from different other posts, but didn’t manage to add it correctly into mine (above). Thanks

To limit the number of images per session or number per submission? (I know you said “session” but want to be sure, because it’s completely different code)

@jonatandor35 Per Session. If I’m getting the terminology right here, my code (above) enables ‘Upload’ into a Dataset rather than a ‘Form Submission’. BTW -I was looking to do it via session tokens, but would be happy to hear if there are better options on how to limit a non-member users’ upload count into a dataset/gallery. I’m sure users can workaround session limits etc, but a basic limitation will do the job.

So it should be like this:

Import { session } from 'wix-storage';
let numberOfSessionImages = 0;
function getNumberOfImages(){
 const sesionalData = session.getItem("numberOfImage");
 if(sesionalData){
 numberOfSessionImages = Number(numberOfSessionImages );
 }
 if (numberOfSessionImages > 1) {$w("#uploadButton1").disable();}
}
$w.onReady(() => {
 getNumberOfImages();
}
//inside the button_click function:
//...code...
wixData.insert("DD", toInsert)
.then( (results) => {
 let item = results; 
numberOfSessionImages++;
session.setItem("numberOfImage", numberOfSessionImages);
getNumberOfImages();
$w("#dataset1").refresh(); 
});
//...code

@jonatandor35 Works like a charm! Many thanks!

You’re welcome :slight_smile: