How can I limit the file type in the file view menu in AsyncFileUpload in ASP.NET AJAX Control Toolkit

I would like to limit what they see in the file download dialog box, which is set to "All files" by default. I understand how to claim that they only download a specific type of file, and that is not a question. I just wanted to know how to set the file type in the file selection dialog.

Is there a way to change this to β€œPNG only” or β€œ* .png”?

This uses AsyncFileUpload in the ASP.NET AJAX Control Toolkit.

+6
visual-studio ajaxcontroltoolkit
source share
3 answers

The current version of ajax management tools does not have this option.

But it’s good that you can get the source code and add a property that handles this.

+2
source share

This one works for me (thanks DavRob for the inspiration).

<cc1:AsyncFileUpload ID="FileUpload" runat="server" OnClientUploadStarted="AssemblyFileUpload_Started" /> <script> function AssemblyFileUpload_Started(sender, args) { var filename = args.get_fileName(); var ext = filename.substring(filename.lastIndexOf(".") + 1); if (ext != 'png') { throw { name: "Invalid File Type", level: "Error", message: "Invalid File Type (Only .png)", htmlMessage: "Invalid File Type (Only .png)" } return false; } return true; } </script> 
+10
source share

You can use the OnClientUploadStart property in the control to run the JavaScript function to check, for example:

 <cc1:AsyncFileUpload ID="FileUpload" runat="server" OnClientUploadStarted="checkExtension" /> 

Then enter the script on your page or include:

 function checkExtension(sender, args) { var ext = args.get_fileName().substring(filename.lastIndexOf(".") + 1); if (ext != 'png') { args.set_cancel(true); //cancel upload args.set_errorMessage("File type must be .png"); //set error message return false; } return true; } 

In this case, we simply use the various bits of the client-side API to get / check the extension, return false and stop downloading / setting the error message (optional) if it is not valid.

+3
source share

All Articles