views:

2165

answers:

4

I am using the new AsyncFileUpload control from the latest AjaxControl ToolKit. My query is regarding the OnClientUploadStarted event which is fired before the upload is started. Is there any way to cancel the upload, as I am checking the fileExtension at this point and would like to cancel the upload so that it does not continue and go on to upload the file. My end result is allow only images to be uploaded. Please advise and thanks for your time.

+1  A: 

You might try adding a "Regular Expression Validator" to the field, and see if you can use that to validate the file selected before the upload starts.

Mitchel Sellers
I tried adding a custom validator, however the validation event is not fired until the form is submitted, however the file is uploaded instantaneously once selected. Hence, my option is to use the OnClientUploadStarted event
Sloane
You mean a Regex validator can't suffice? It has built-in client-side validation. If you build your own custom validator, you can attach a client-side function as well.
o.k.w
+1  A: 

Got the answer, all I had to do was override the javascript function with this script(not the best answer, but works), you all could have done faster and cleaner

var orig = AjaxControlToolkit.AsyncFileUpload.prototype.raiseUploadStarted;
AjaxControlToolkit.AsyncFileUpload.prototype.raiseUploadStarted = function(e) {
var evt = this.get_events()._getEvent('uploadStarted');
if (evt) {
    if (evt.length > 1)
        return orig(e);
    else if (evt.length === 1)
        return evt[0](this, e);
    }
}
Sloane
A: 

Hi, I have exactly same issue... can you be more descriptive.

What this overriding is doing here.

AmiT
The Ajax Control Toolkit has had an update for the AsyncFileUpload control. I have not checked the new one out but I hope this issue is fixed in the newer version.As explained before, the return values from the function you attach to OnClientUploadStarted event are not used at all. You can check by debugging the javascript, this particular function originally just calls the function and not use the return value(true/false use false to stop the upload). This override ensures the return values are used by the internal event.
Sloane
Here is the link for all the updateshttp://ajaxcontroltoolkit.codeplex.com/SourceControl/ListDownloadableCommits.aspx
Sloane
A: 

kindly tell me where to put this code as i want to cancel the Upoload if file is not a image file within desired Size

Ankur