Display File not supported message while uploading exe files


  • Hi,

    Uploading a file type that is not supported (.exe, .chm, ) is causing the site to throw an application error.  How to block these file types and provide a error that the file type is not supported.

    Should this be done through itemadding event handler or is there any other way to handle it.

    Thank you

    Monday, September 12, 2011 6:12 AM

All replies

  • Yes, you can do this by ItemAdding event handler for server side validation. Here i suggest you client side validation by Javascript. Please go through below articles for this

    Use regular expression validator for this, refer below article

    w: | t: @sharesandip
    • Marked as answer by Venkatzeus Monday, September 12, 2011 6:48 AM
    • Unmarked as answer by Venkatzeus Wednesday, September 14, 2011 2:38 PM
    Monday, September 12, 2011 6:40 AM
  • Hi,

    I tried this:

    string beforeUrl = properties.BeforeUrl;

    string extension = beforeUrl.Substring(beforeUrl.LastIndexOf('.') + 1);

    if ((extension.ToLower().Equals("exe")) || (extension.ToLower().Equals("chm")))


    properties.Status = SPEventReceiverStatus.CancelWithError;

    properties.ErrorMessage = "Please upload a valid document.";

    properties.Cancel = true;



    I am still getting the application error page. Am i missing something here.

    I have set debug to false, and callstack set to true. I need to set callstack to true for some other purpose, so i will not able to change to false.

    • Edited by Venkatzeus Tuesday, September 13, 2011 12:24 PM
    Tuesday, September 13, 2011 12:22 PM
  • Hi Venkatzeus,


    Thanks for your post.

    Please try to unblock the file type in Central Administration at first, and then you can use the ItemAdding Event Handler to block the file types and show custom error message.

    More information:

    Friday, September 23, 2011 6:45 AM
  • Hi,

    Thank you very much for the reply.

    Even i thought the same. However, if the file extensions increase.. then the list would go for a toss.

    It is better blocking it from central Admin and it would be great if there was a way to show customized messages.


    Friday, September 23, 2011 3:25 PM
  • I wud still vote for a javascript solution .. to wire up funciton on upload button for showing the messages ... 
    "T" |
    Friday, September 23, 2011 5:58 PM