Check document Filename length before uploading

Hello,

is there any possibily to check the filenamelength before uploading? For example if it is smaller then 200, the upload is okay. If it is greater, equal 200 the upload is denied and an alert should be disblayed.

Here is my code.

fd.control(listOrLibrary).$on('filesUploaded',
    async function(itemIds) {
        const library = await sp.web.lists.getByTitle(docLibraryTitle);
        const batch = await pnp.sp.web.createBatch();
        const siteNr = location.pathname.match(/\d{5}/)[0];

        for(var i = 0; i < itemIds.length; i++){
            //Change file name
            const currentFile = await library.items.getById(itemIds[i]).select('File').expand('File').get()
            const fileName = currentFile.File.Name;
            const fileType  = fileName.match(/\.[0-9a-z]+$/i)[0]; 

            const newFileName = fileName.replace(fileType, '') + '__' + itemIds[i] + fileType;
            const currentItem =await library.items.getById(itemIds[i]);

            currentItem.inBatch(batch).update({
                FileLeafRef: newFileName, Filename: fileName, Data: fd.field('Attachments_x0020_document_x0020').value, Description0: fd.field('Description').value
            });
            if(newFileName.length >=200){
                alert('This file couldn´t be uploaded. Please check the length of the filename and change it.');
            }
        }
        batch.execute().then(function(){
            fd.control(listOrLibrary).refresh();
         });
}); 

I get the filenamelength but I don´t know how to denie (don´t allow) to upload the document.
Another way could be to delete the document, if I can´t denie the upload.

I hope you can help me.

Best regards
Sternchen

Hello @Sternchen,

You can add custom validation to the form, thus if the file name is longer than 200 characters user will get an error.

Or you can add the code that will delete the file if the file name is longer than 200 characters.

Please see the code example in this post: