This is a migrated thread and some comments may be shown as answers.

RadAsyncUpload OnClientValidationFailed triggering for no reason

3 Answers 254 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
nico
Top achievements
Rank 1
nico asked on 24 Jul 2017, 12:53 PM

I have a RadAsyncUpload and the function for OnClientValidationFailed is getting called with a wrong file size error even though the file is well within the size limit.

Markup

<telerik:RadAsyncUpload runat="server" ID="fUploader" PostbackTriggers="btnSendEmail,btnSaveDraft" OnClientValidationFailed="fileUploadValidationFailed"></telerik:RadAsyncUpload>

server side

fUploader.TargetFolder = uploadPath
fUploader.AllowedFileExtensions = New String() {"*.jpg", "*.png", "*.gif", "*.jpeg", "*.bmp", "*.tiff", "*.pdf", "*.gpx", "*.tcx"}
fUploader.MaxFileSize = 2500000

javascript

function fileUploadValidationFailed(sender, args) {
    var fileExtention = args.get_fileName().substring(args.get_fileName().lastIndexOf('.') + 1, args.get_fileName().length);
    if (args.get_fileName().lastIndexOf('.') != -1) {//this checks if the extension is correct
        if (sender.get_allowedFileExtensions().indexOf(fileExtention.toLowerCase()) == -1) {
            alert("File type selected is not allowed.  Valid file types are .jpg, .png, .gif, .jpeg, .bmp, .tiff, .pdf, .gpx, .tcx");
        }
        else {
            alert("File too large.  Max file size 2 MB.");
        }
    }
    else {
        alert("File type selected is not allowed.  Valid file types are .jpg, .png, .gif, .jpeg, .bmp, .tiff, .pdf, .gpx, .tcx");
    }
}

web.config

<system.web>
    <httpRuntime maxRequestLength="8192"/>

3 Answers, 1 is accepted

Sort by
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 27 Jul 2020, 01:25 PM

Did you ever resolve this?  I have just started getting the same behavior.  I do see that you have put in the extensions like 

*.jpg, *.png etc.  with wildcard asterisks.  I am not sure those are permitted int he AllowableFileExtensions - should just be .jpg,.png etc. and others have mentioned that spaces between the extensions can be a problem too.... 

0
Peter Milchev
Telerik team
answered on 28 Jul 2020, 11:30 AM

Hello,

The server-side default check for the extension is a below:

foreach (var ext in fUploader.AllowedFileExtensions)
{
    if (fileExtension.ToLower().Trim('.') == ext.ToLower().Trim('.'))
    {
        return true;
    }
}

The fileExtention.ToLower() is something like ".jpg" while the ext.ToLower() will be "*.jpg" so that is the reason the incorrect file extension is expectedly thrown.

As Allen has correctly suggested, the allowed extensions need to be with no spaces and no characters before the dot.

New String() { ".jpg", ".png", ".gif", ".jpeg", ".bmp", ".tiff", ".pdf", ".gpx", ".tcx" }

With that said, the more accurate client-side validation is done by splitting the string to an array:

var allowedExtentions = sender.get_allowedFileExtensions().split(",");

if (args.get_fileName().lastIndexOf('.') != -1) {//this checks if the extension is correct
       if(allowedExtentions.indexOf(fileExtention.toLowerCase()) == -1) {

Regards,
Peter Milchev
Progress Telerik

0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 28 Jul 2020, 12:47 PM

Typo:  Extentions everywhere needs to be Extensions

 

Fixup (using Linq)

var xt = ".pdf,  .doc,docx,     .xls"// configurable extensions for particular type(s) of documents in web.config, so these are a mess, need to standardize for RadAsyncUpload.AllowablFileExtensions
var fixedExt = xt.Split(',').Select(x => (x.Trim().StartsWith(".") ? x.Trim() : string.Concat(".", x.Trim())));
//fixedExt.Dump(); // LinqPad https://www.linqpad.net/
//Value
//.xls
//.pdf
//.docx
//.doc

 

Tags
AsyncUpload
Asked by
nico
Top achievements
Rank 1
Answers by
Allen
Top achievements
Rank 2
Iron
Veteran
Peter Milchev
Telerik team
Share this question
or