Limit image upload file size

I’m working with a client who allows users to upload image files to a database through a custom form. Now over time, these will obviously build up. I’m looking for a way to possibly limit the size of the files that they upload.

I’ve had a look through the API documents and reading different articles, but I’m stumped (not a coder by any stretch of the imagination)

Here’s what I’ve got so far, it doesn’t appear to make a difference:

$w.onReady(function () {
    $w("#uploadButton13").onCustomValidation( (value, reject) => {
      let files = $w('#uploadButton13').value;
        files.forEach((file) => {
            console.log(file.size);  
        if(File[0].size > 0.1) { reject("File size exceeds 0.1") ;  }

        else { console.log('File size:', File[0].size) }
})
    })
})

Any help is appreciated.

You’re in the right direction but the size limit you chose doesn’t make any sense since ALL the files are always bigger than 0.1 byte.

Also you wrote File instead of file (lowercase)
Also remove the [0]. It is not an array.

Oooops! It wasn’t meant to say 0.1 :joy:

So, if these are changed then it should work, right?

@noahlovell It should work.

Hello @jonatandor35 , I would like to know about this code in detail. It would be a big help. I have searched many pages but failed to find solution. My conceren is I want my clients to upload image in a restricted manner where file size should be under 2 mb. I saw this post and curious to know above posted code. I have changed some code according to your instruction. Like :


$w.onReady(function () {
    $w("#uploadButton13").onCustomValidation((value, reject) => {
        let files = $w('#uploadButton13').value;
        files.forEach((file) => {
            console.log(file.size);
            if (file.size > 2000000) { reject("File size exceeds 2000000"); } else { console.log('file size:', file.size) }
        })
    })
})

Now second question is if above code is correct, how this code works. Is this code works with a button single handedly or we need additional upload button for it ! I am counfused. Thanks in advance : )

@onlinesanawad Hi, sorry I haven’t been here for a while.
I’ll guess you man 200 mega byte (MB) and not 200 mega bit (Mb).
1 mega byte is 1024 * 1024 bytes so your calculation is not completely accurate.

Use a single upload button for it. The value array contains the files and their sizes.

@jonatandor35 Suppose I have a upload button allowing a single file/image to upload. I want to restrict user to upload under 2 megabyte file size / image size only. If file is having greater mb upload fails and give error caution. Is it possible? Thanks : )

@onlinesanawad yes. Use the code you posted but have it like:

 if (file.size > 2097152) { reject("File size exceeds 2MB"); }

(2MB = 2,097,152 bytes)

@jonatandor35 Thank you, I haven’t tested this code. I will test asap. Will get back to you if any problem arise in that. Thanks again : )