How to programmatically validate before file upload

3,180 views
Skip to first unread message

Adan Enrique Jauregui Ortega

unread,
Sep 26, 2012, 7:21:38 PM9/26/12
to jquery-f...@googlegroups.com
Hi, i've a problem when i'm implementing validations.

Okay, i don't know how to get started. :)

I'd like to manipulate the Add callback in order to achieve custom file validation, and if it fails, try to use the upload template to render my custom error.

Today, i've been using the download template and the Send callback in order to render custom validations on client side, however, it´s so vulnerable use this way. (invalidated options, skip restrictions, 'unknown' file uploads, and others)

I'll thank you if you help me.

My development platform: Microsoft ASP .NET under .NET Framework 4.0

I'm using this version: 5.0.13

Later.

Nguyen Kien

unread,
Sep 27, 2012, 11:17:32 PM9/27/12
to jquery-f...@googlegroups.com
I don't know exactly what you're looking for, but here are some good start:
For custom message in error, try 

//local messages
locale = {
    "fileupload": {
        "errors": {
            "maxFileSize": "File is too big",
            "minFileSize": "File is too small",
            "acceptFileTypes": "Filetype not allowed",
            "maxNumberOfFiles": "Max number of files exceeded",
            "uploadedBytes": "Uploaded bytes exceed file size",
            "emptyResult": "Empty file upload result"
        }      
    }
};


If you want to further control when file is added, bind your own function to the event:

 $('#fileupload').bind('fileuploadadd', function (e, data) {
var acceptFileTypes = $(this).data('fileupload').options.acceptFileTypes;
$.each(data.files, function (index, file) {            
if (!(acceptFileTypes.test(file.type) ||
                    acceptFileTypes.test(file.name))) {
console.log(file.name +' not accepted');
//remove
//data.files.splice(index,1);
}
        });
   });

However, keep in mind that all client validations can be voided. They're there for usability only. Enforce validation on server-side as well.

J. Wolkerstorfer

unread,
Oct 30, 2012, 9:30:27 AM10/30/12
to jquery-f...@googlegroups.com
Which file should have the custom error messages in it in order to see them when the upload and download templates are rendered? I've tried putting them inside the templates, and in a separate script tag on the same page, with no luck. I can't find any references in the code to suggest which separate .js file to use for the custom messages. (I'm using the jquery-ui branch of the project, in case that makes a difference.)

Thank you!

Nguyen Kien

unread,
Oct 30, 2012, 10:25:22 AM10/30/12
to jquery-f...@googlegroups.com
I've attached a sample code of mine (basic version with a little mix of UI). I made this so that I could quickly integrate with any templates. In this one, the uploader will check for file which are images, and will give an alert if user uploads the wrong file type. It's simple enough to know what's going on. All the scripts needed are inline code.
img_upload.zip

J. Wolkerstorfer

unread,
Oct 30, 2012, 2:22:54 PM10/30/12
to jquery-f...@googlegroups.com
Thank you! I've got it working now.

Safaa AlNabulsi

unread,
Apr 2, 2015, 11:54:26 AM4/2/15
to jquery-f...@googlegroups.com

please I have the same problem and I can't download/view ur file :(
Reply all
Reply to author
Forward
0 new messages