Hi everyone,
I am trying to write the code to limit the size of the image file that User can upload and refresh the photo after loaded.
Can you please tell me where I’m wrong and what’s missing? In fact doesn’t works good and show me problem with “size”
Thanks in advance, as always
export function BBrochureUpload_change(event) { . //button to upload event
let mySizeLimit = 2000; //Mb limited
let files = $w(“#BBrochureUpload”).value; //button to upload
let fileSize = files[0].size;
if (fileSize < mySizeLimit) {
$w(“#BBrochureUpload”).startUpload()
.then((uploadedFile) => {
$w(“#image1”).expand(); // images show after upload
})
. catch ((uploadError) => {
console.log(File upload error: ${uploadError.errorCode}
);
});
} else {
$w(“#image1”).collapse();
$w(“#TLimited”).show(); // write " Image to large to upload"
}
Sorry, but I know that tutorial and it’s not about what I asked for.
The tutorial does not mention how to refresh the upload of an image after uploading, nor how to limit the size of the file before uploading it. You can please tell me instead how to correct my code that I reported above.
Thank You
I can’t tell you where you’re wrong since you don’t say what’s not working - “doesn’t works good” is not enough information to go on.
What problem are you having with size? From your code I see that you are limiting upload to a files less than 2000 bytes. Do you really have image files that small? See the File object API for more information.
Also, what do you mean by “refresh the upload of an image…”?
See Typical File Upload Scenario .
Hi @yisrael-wix
I deliberately chose a low limit to check the effects of code.
The code does not show me any error either in compilation, nor in the preview and test phase.
The code should prevent a file from being uploaded when the size exceeds the limit and write “File to big”. In the contrary when it is the correct size it should load and show the image.
The problem that also accepts 1Mb files (more than the limit) and does not indicate an error, but shows me the “File to big” message.
Can Yoo now help me, please?
@info60119 It might be that you are comparing number with string.
As you can see in the File object API , the size is represented as a string. If you want to compare with a number, you first need to convert the string to a number, and then compare. Here’s one way to convert the size to a number:
let fileSize = parseInt(files[0].size);
Hi @yisrael-wix
If I used yours metod the editor say "missing radix parameter, and however don’t works.