locked
Restrict file types - Data Validation - Browser Forms RRS feed

  • Question

  • Hi

    I have created a number of browser enabled InfoPath forms. I have also tried to restrict users from attaching files which do have the extension .pdf or .tif numerous times without success.

    I tried programatticaly to do this and for some reason the code did not work. I now have tried use Data Validation which I need assistance with. I use three data vlaidation rules.

    1st rule  - checks if a fields equals yes and the attachment is blank - a screentip appears reminding the user to attach .tif or .pdf.
    2nd rule - checks if a field equals yes and the file attachment control does not contain .pdf - a screentip appears reminding the user to attach .tif or .pdf.
    3rd rule - checks if a field equals yes and the file attachment control does not contain .tif - a screentip appears reminding the user to attach .tif or .pdf.

    For some strange reason, the data validation works with certain pdf files and I cannot understand why this would happen. When I attach files that I scanned as a pdf the screentip appears however if I attach a file from my desktop the data valdiation works.

    Does anyone have an idea why this would happen? I seem to think there may be an error with the does not contain .pdf part of the data validation rule. Is anyone aware of an easy way to restrict file types in a browser enabled form.

    Thanks very much

    Regards
    Tony

    Wednesday, February 23, 2011 9:25 AM

Answers

  • Ok, but you have "'" caracter on each line that means that is code is commented...
    Have you try to pass in debug mode to understant why it not works.

    So, if it not work, you can use another method.

    • Use the "CHANGED" event on the attachment (and not the CHANGING).
    • Use the previous code to get extension and put the result in a field xml of your form (called "txtExtension" for example) without the line:

          if (extension != "pdf" && extension != "tif")
               {     e.ReportError(e.Site,false,"Only PDF and TIF FileTypes are allowed.");    }

    • Use a conditional formatting in your form to show an error message when the field "txtExtension" is equals to pdf or tif.

    I think this method will work with no problem

     


    Visit my blog : http://rmatayron.blogspot.com/
    • Marked as answer by Peng Lei Tuesday, March 1, 2011 8:41 AM
    Wednesday, February 23, 2011 2:27 PM

All replies

  • Hi Remi

    Thanks for your reply. That post was exactly what I followed. Conditional formatting is suggested however this is not available for browser forms and instead of using 'ends with', I used 'does not contain' in my data validation rules.

    I will attempt the code again. Please let me know if there is any way to enable this. It works for specific pdf's though.

    Thanks
    Tony 

    Wednesday, February 23, 2011 9:57 AM
  • Hi

    I tested the following code again which failed.

    Public Sub field83_Validating(ByVal sender As Object, ByVal e As XmlValidatingEventArgs)
          ' if (e.Site.Value != "")
          '  {  byte[] attachmentNodeBytes = Convert.FromBase64String(e.Site.Value.ToString());
          ' int fnLength = attachmentNodeBytes[20] * 2;
          '     byte[] fnBytes = new byte[fnLength];
          '     for (int i = 0; i < fnBytes.Length; i++)
          '     {  fnBytes[i] = attachmentNodeBytes[24 + i];}
          'char[] upcharFileName = System.Text.UnicodeEncoding.Unicode.GetChars(fnBytes);
          '        string upfileName = new string(upcharFileName);
          '       int extIndex = upfileName.LastIndexOf(".") + 1;
          '       string extension = upfileName.Substring(extIndex, upfileName.Length - extIndex);
          '      extension = extension.Replace("\0", "");
          '      if (extension != "pdf" && extension != "tif")
          '     {     e.ReportError(e.Site,false,"Only PDF and TIF FileTypes are allowed.");    }
        End Sub

    I really need to restrict file types and I have spent countless hours trying to set this up. No errors were received for the above code and the form was published as an administrator approved template. I upload an Excel spreadsheet and no error is received.

    Appreciate any assistance.

    Thanks
    Tony

    Wednesday, February 23, 2011 11:18 AM
  • Ok, but you have "'" caracter on each line that means that is code is commented...
    Have you try to pass in debug mode to understant why it not works.

    So, if it not work, you can use another method.

    • Use the "CHANGED" event on the attachment (and not the CHANGING).
    • Use the previous code to get extension and put the result in a field xml of your form (called "txtExtension" for example) without the line:

          if (extension != "pdf" && extension != "tif")
               {     e.ReportError(e.Site,false,"Only PDF and TIF FileTypes are allowed.");    }

    • Use a conditional formatting in your form to show an error message when the field "txtExtension" is equals to pdf or tif.

    I think this method will work with no problem

     


    Visit my blog : http://rmatayron.blogspot.com/
    • Marked as answer by Peng Lei Tuesday, March 1, 2011 8:41 AM
    Wednesday, February 23, 2011 2:27 PM