Link to home
Start Free TrialLog in
Avatar of Devildib
Devildib

asked on

Asp.net file upload validation using javascript and C#

Hi experts,

I have an asp.net webform on which i have various controls like text boxes and a file upload control with browse textbox , button and an upload button.I have a validation check in place for gif image uploads only.When i upload an image, the image gets displayed in an image control.Now, as a negetive testcase, when i manually enter a something in the browse textbox like abc.gif, the extension check gets by passed in javascript.And control doesnot pass on to the c# upload click event.Can you provide some logic for complete validation client and server side?I need to do this using javacsript and c# only.
SOLUTION
Avatar of jorge_toriz
jorge_toriz
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Devildib
Devildib

ASKER

CAn you provide the javascript function instead of jquery please?
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
getting files.length is null or not an object.
changed function:

var Upload = {};
Upload.validateExtension = function(o, e) {
    if (getel("fuFile").files.length > 0)
        e.IsValid = getel("fuFile").files[0].type === 'gif';
    else
        e.IsValid = true;
}
Upload.validateSize = function(o, e) {
    if (getel("fuFile").files.length > 0)
        e.IsValid = getel("fuFile").files[0].size > 0;
    else
        e.IsValid = true;
}

Open in new window

so ?
Above code is giving errors as mentioned above
the error is the code don't find the element in the page so we get null and we try to get length of this "null"...

    if (getel("fuFile") && getel("fuFile").files.length > 0)

Open in new window


or better, use  : getel("<%= fuFile.ClientID %>")
    var fuFile = getel("<%= fuFile.ClientID %>");
    if (fuFile && fuFile.length)

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am sorry, forgot to mention that the getel function i was using is doing nothing but appending the master control text and ultimately making up client id's.The problem is different.On upload button click,i do the validation and the valid gif is displayed nice and fine in the image server control for preview.The file path gets wiped out since its a postback.Now on final submit button of the form, how do i make sure on client side if the image control holds anything?I cannot check the file upload control since upload has already been clicked and file is getting displayed in the image control.
The fufile.length thing yiu mentioned worked.Just that the final submit button should be vakidating the preview image existance in the inage control.one more thing, when i manually enter an arbitary file name with gif extension, client side validation doesnot validate this.And button click event does'nt fire whether its upload button or final submit button.Why dies this happen?
Well, as far as I know you can't validate the content of a file directly from the client side, I mean that the extension is the only thing you could validate, so if you put a file with that extension, the client side will say "it's fine for me"
Ok