Restrict files with no extension

5 posts, 1 answers
  1. WAMatt
    WAMatt avatar
    17 posts
    Member since:
    Sep 2009

    Posted 14 Sep 2012 Link to this post

    Greetings,

    I would like to allow users to upload any type of file except ones with no extension.  Any ideas how to do this?

    Your help is appreciated!

    Matt
  2. Answer
    Plamen
    Admin
    Plamen avatar
    2734 posts

    Posted 18 Sep 2012 Link to this post

    Hello Matt,

     
    One way to achieve such validation is to check the uploading file's name in the OnClientFileUploading event as in the code below:

    function OnClientFileUploading(sender, args) {
                    var file = args.get_fileName();
                    var extension = file.substr((file.lastIndexOf('.') + 1));
                    if (extension == file) {
                        args.set_cancel(true);
                    };
                }

    Hope this will help you solve the issue.

    Kind regards,
    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. WAMatt
    WAMatt avatar
    17 posts
    Member since:
    Sep 2009

    Posted 18 Sep 2012 Link to this post

    Thank you, that got me part-way there.  I am still unsure how to handle the file to prevent the upload.  Here is what I am doing...

    JavaScript...
    <script type="text/javascript">
      function uplFileUpload_OnClientFileUploading(sender, args) {
        var file = args.get_fileName(); var
        extension = file.substr((file.lastIndexOf('.') + 1));
        if (extension == file) {
          args.set_cancel(true);           
        }
      }
    </script>

    ASPX...
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
      <tr class="pulldown_fields">
        <td width="30%" style="font-weight: bold" align="right">
          Upload Files
        </td>
        <td width="3%">
            
        </td>
        <td>
          <telerik:RadAsyncUpload ID="uplFileUpload" runat="server" UploadedFilesRendering="BelowFileInput"
            MultipleFileSelection="Automatic" OnClientFileUploading="uplFileUpload_OnClientFileUploading">
          </telerik:RadAsyncUpload>
          <br />
          <telerik:RadButton ID="btnFileUpload" runat="server" Text="Save Files" Icon-PrimaryIconCssClass="rbSave"
            OnClick="btnFileUpload_Click">
          </telerik:RadButton>
          <asp:Label ID="errFileUpload" runat="server" ForeColor="Red" />
        </td>
      </tr>
    </table>

    CP...
    protected void btnFileUpload_Click(object sender, EventArgs e)
    {
      saveAttachments(1000);
    }
    private void saveAttachments(int folderNumber)
    {
      resetValidation();
      errFileUpload.Text = "";
      if (!Directory.Exists(Server.MapPath("~/Uploads/" + folderNumber.ToString())))
      {
        Directory.CreateDirectory(Server.MapPath("~/Uploads/" + folderNumber.ToString()));
      }
      if (uplFileUpload.UploadedFiles.Count > 0)
      {
        try
        {
          foreach (UploadedFile file in uplFileUpload.UploadedFiles)
          {
            file.SaveAs(Server.MapPath("~/Uploads/" + folderNumber.ToString() + "/" + file.GetName()), true);
            saveChangeLog("DeceasedMember",
                      Convert.ToInt32(hdnDeceasedMemberID.Value),
                      file.GetName() + " has been uploaded");
          }
          errFileUpload.Text = uplFileUpload.UploadedFiles.Count.ToString() + " file(s) were uploaded";
        }
        catch (Exception)
        {
          errFileUpload.Text = "Unable to upload file(s)";
        }
      }
      else
      {
        errFileUpload.Text = "Select a file first";
      }
    }

    It flags the file in the uploaded files list with a red dot, but it still allows it to be uploaded when I click the Save button.  What am I missing?

    Thanks again!

    Matt
  5. Plamen
    Admin
    Plamen avatar
    2734 posts

    Posted 21 Sep 2012 Link to this post

    Hi Matt,

     
    This issue is a known limitation observed only in the Silverlight Upload module and if you want to avoid it you can add the following javascript to the page:

    Telerik.Web.UI.RadAsyncUpload.Modules.Silverlight.isAvailable = function() { return false; }

    Hope this will help you solve the issue.

    Greetings,
    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. WAMatt
    WAMatt avatar
    17 posts
    Member since:
    Sep 2009

    Posted 24 Sep 2012 Link to this post

    This gives me an error: "Telerik is not defined".  I went ahead and dealt with this using server-side code...
    private void saveAttachments(int folderNumber)
    {
      resetValidation();
      errFileUpload.Text = "";
      if (!Directory.Exists(Server.MapPath("~/Uploads/" + folderNumber.ToString())))
      {
        Directory.CreateDirectory(Server.MapPath("~/Uploads/" + folderNumber.ToString()));
      }
      if (uplFileUpload.UploadedFiles.Count > 0)
      {
        try
        {
          int cnt = 0;
          string msg = "";
          foreach (UploadedFile file in uplFileUpload.UploadedFiles)
          {
            msg = "";
            if (file.GetExtension() != "")
            {
              cnt++;
              file.SaveAs(Server.MapPath("~/Uploads/" + folderNumber.ToString() + "/" + file.GetName()), true);
            }
            else
            {
              msg = " All uploaded files must have an extension.";
            }
          }
          errFileUpload.Text = cnt.ToString() + " file(s) were uploaded." + msg;
        }
        catch (Exception)
        {
          errFileUpload.Text = "Unable to upload file(s)";
        }
      }
      else
      {
        errFileUpload.Text = "Select a file first";
      }
    }


    Matt
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017