Hi Team,
I want to validate the file name before uploading a file. I tried to cancel the file upload in FileSelected & FileUploading event, but it's not working. Please let me know what is the issue in my code. I have attached for your reference.
Requirement:
1. Need to cancel the file upload, if the length of the filename exceed certain limit.
2. If a dropdown value is not selected, then alert the user to select the dropdown and cancel the upload.
3. After cancelling the fileupload, i should able to select another file. Now it's getting disabled.
Thanks,
Karthick.K
Hello Karthick ,
Thank you for posting in our forum.
The fileSelected event is not cancelable, so it won’t prevent the file from uploading.
Canceling file uploading also might not work in scenarios where the file size is small and can be uploaded in a single request, since the fileUploading event is fired during the upload process and is fired while the file is uploading. So for a small file canceling the event on the client side will not suffice since the file will be uploaded to the server before the fileUploading event is fired.
Instead you can handle the server side UploadStarting event and cancel the uploading there:
protected void WebUpload2_UploadStarting(object sender, UploadStartingEventArgs e)
{
if (e.FileName.Length > 15)
e.ServerMessage = "File Name Exceeded :" + e.FileName.Length;
e.Cancel = true;
}
You can then show the custom server message on the client side on the OnError client event:
function WebUpload2_OnError(eventArgs, infoObject) {
if (infoObject.serverMessage != "") {
alert(infoObject.serverMessage);
…
You won’t be able to cancel the event based on the selected value of another control on the page since the requests to the server for uploading the file are done via independent ajax calls that hold only the file information and data. So you would not have access to the updated values of other controls during the UploadStarting event. Instead I suggest that you check the selected value on the FileSelecting event, which is a cancelable event.
If there’s no selected value from the drop down on the page you can cancel that event and show an alert message.
I’ve attached a sample for your reference. Let me know if you have any questions or concerns.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://www.infragistics.com/support
Hi Maya,
Thanks for the reply.
Dropdown event is working fine, but file name length verification is not working. It's not alerting the message set in UploadStarting. It actually calling Uploading event after setting the serverMessage in UploadStarting event. In uploading event, the serverMessage is empty and not going to client side error evernt.
Have you had the chance to test the attached sample on your side?
When testing it on my side, if the file name is longer than 15, the event is canceled ( e.Cancel = true;) and the client side OnError event is raised.
If you’re getting a different behavior please let me know the exact version of the control you’re using and the browser and exact browser version you’re encountering the issue on.
I encountered the issue while testing the sample only. I am using IE 11 (11.0.9600.17126) and Infragistics (14.1.20141.1015) which is same in the attached sample.
If the file name is longer than 15, it's not uploading the file, but am not getting the alert as well.
I was able to reproduce this issue.
It has been fixed in the newest service release version of 14.1: 14.1.20141.2150.
You can download the latest service release for your version by following these steps:
Go to http://www.infragistics.com/ and log in with your account.
Select “Account”. This will open the “My keys and downloads” page.
Select your product from the product list.
Select the “Service releases” tab .
Here you will find the latest service release available for download.
Download and install the service release, make sure you upgrade the attached sample so that it references the new version and the new scripts and css files for the WebUpload.
Let me know if you’re still encountering any issues afterwards.
Thanks for the details. I will check this version and let you know if there are any issues.
There is one small issue, if the file name length is 203+, the alert is not working. The file is not getting uploaded, but alerts are not showing.
Eg:
15_Pages_ViewPagePerson15_Pages_ViewPagePerson.85012345678601234567870123456788012345678901234567100123456711012345671201234567130123456714012345671501234567160123456717012345671801234567190123456712.txt
Hello Karthick,
I’ve tested this on my side with version 14.1.20141.2150 and didn’t encounter any issues. The alert message was displayed as expected for a file with 203 characters in its name :
Please test this with the latest service release on your side and let me know if you’re still encountering any issues.
I’m just following up to see if you’ve been able to resolve your issue. If you have any questions or concerns or if you need further assistance please let me know.
Developer Support Engineer
Thanks Maya for the support. The issues are resolved. I haven't used the latest version yet to test the length issue.