Client-side set allowed file extensions

6 posts, 1 answers
  1. Scott
    Scott avatar
    35 posts
    Member since:
    Sep 2012

    Posted 05 Apr 2013 Link to this post

    I was hoping to set the allowed file extensions based on some chosen radio buttons, but the sample code in the on-line documentation does not work. I am weak in my javascript, so I am probably missing something simple. Here's the documentation page:
    http://www.telerik.com/help/aspnet-ajax/asyncupload-client-object.html

    and here's the sample code it provides:
    function AllowJPEGExtension() {
      var upload = $find("<%= RadAsyncUpload1.ClientID %>");
     
      if (!upload.isExtensionValid(".jpeg"))
      {
         var exts = upload.get_allowedFileExtensions();
         exts[exts.length] = ".jpeg";
      }
    }
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Apr 2013 Link to this post

    Hi,

    I guess you need to allow file extensions based on the radio button selection. Here is the code I tried from server side which worked as expected.

    ASPX:
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"
        AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
        <asp:ListItem Text="jpg"></asp:ListItem>
        <asp:ListItem Text="doc"></asp:ListItem>
        <asp:ListItem Text="pdf"></asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server">
    </telerik:RadAsyncUpload>

    C#:
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string file = RadioButtonList1.SelectedItem.Text;
        RadAsyncUpload1.AllowedFileExtensions = new string[] {file};
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Scott
    Scott avatar
    35 posts
    Member since:
    Sep 2012

    Posted 14 Apr 2013 Link to this post

    Thank you Princy for providing a server-side solution.  Does anyone have a client-side solution?
  5. Answer
    Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 17 Apr 2013 Link to this post

    Hello Scott,

     
    RadAsyncUpload does not support such client change in the allowed extensions because it would cause a security by providing possibility to upload unwanted files. 

    In your scenario since you only want to control this behavior with radio button instead of using the "AllowedFileExtensions" you can use OnClientFileUploading event and cancel the uploading file if its extension is not the desired one as in the code below:

    function OnClientFileUploading(sender, args) {
                   var fileExtention = args.get_fileName().substring(args.get_fileName().lastIndexOf('.') + 1, args.get_fileName().length);
                   alert(fileExtention);
               }

    Please have in mind that this can easily be overriden so if you strongly need this validation you will have to use server-side way described earlier.

    Hope this will explain the issue.
    All the best,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  6. Scott
    Scott avatar
    35 posts
    Member since:
    Sep 2012

    Posted 22 Apr 2013 Link to this post

    Thank you Plamen.  If possible, you may want to request a change to the documentation, since the sample code I mentioned is apparently wrong.
  7. Plamen
    Admin
    Plamen avatar
    2731 posts

    Posted 22 Apr 2013 Link to this post

    Hi Scott,

     
    I already did this. Thank you very much for your concern with Telerik controls.

    All the best,
    Plamen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017