Reputation:
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.
Upvotes: 4
Views: 7235
Reputation: 125
Try this code:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (rdoFileType.SelectedValue == String.Empty)
{
lblStatus.Text = "Please select a file type before selecting a file.";
AsyncFileUpload1.FailedValidation = true;
e.state = AjaxControlToolkit.AsyncFileUploadState.Failed;
return;
}
try
{
// System.Threading.Thread.Sleep(5000);
if (AsyncFileUpload1.HasFile)
{
string _filename = System.IO.Path.GetFileName(e.filename);
System.IO.FileInfo f = new System.IO.FileInfo(AsyncFileUpload1.PostedFile.FileName);
if (rdoFileType.SelectedValue == "F")
{
if (f.Extension != ".pdf")
{
lblStatus.Text = "Final Document must be a .pdf";
e.state = AjaxControlToolkit.AsyncFileUploadState.Failed;
e.statusMessage = "Final Document must be a .pdf";
throw new Exception("Final Document must be a .pdf");
}
Upvotes: 1
Reputation:
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);
}
}
Upvotes: 4
Reputation: 63136
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.
Upvotes: 1